0
7
0
39
37
47
45
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 demos |
| 20 | 20 |
///\file |
| 21 | 21 |
///\brief Demonstrating graph input and output |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program gives an example of how to read and write a digraph |
| 24 | 24 |
/// and additional maps from/to a stream or a file using the |
| 25 | 25 |
/// \ref lgf-format "LGF" format. |
| 26 | 26 |
/// |
| 27 | 27 |
/// The \c "digraph.lgf" file: |
| 28 | 28 |
/// \include digraph.lgf |
| 29 | 29 |
/// |
| 30 | 30 |
/// And the program which reads it and prints the digraph to the |
| 31 | 31 |
/// standard output: |
| 32 | 32 |
/// \include lgf_demo.cc |
| 33 | 33 |
|
| 34 | 34 |
#include <iostream> |
| 35 | 35 |
#include <lemon/smart_graph.h> |
| 36 | 36 |
#include <lemon/lgf_reader.h> |
| 37 | 37 |
#include <lemon/lgf_writer.h> |
| 38 | 38 |
|
| 39 | 39 |
using namespace lemon; |
| 40 | 40 |
|
| 41 | 41 |
int main() {
|
| 42 | 42 |
SmartDigraph g; |
| 43 | 43 |
SmartDigraph::ArcMap<int> cap(g); |
| 44 | 44 |
SmartDigraph::Node s, t; |
| 45 | 45 |
|
| 46 | 46 |
try {
|
| 47 |
digraphReader("digraph.lgf"
|
|
| 47 |
digraphReader(g, "digraph.lgf"). // read the directed graph into g |
|
| 48 | 48 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap
|
| 49 | 49 |
node("source", s). // read 'source' node to s
|
| 50 | 50 |
node("target", t). // read 'target' node to t
|
| 51 | 51 |
run(); |
| 52 | 52 |
} catch (DataFormatError& error) { // check if there was any error
|
| 53 | 53 |
std::cerr << "Error: " << error.what() << std::endl; |
| 54 | 54 |
return -1; |
| 55 | 55 |
} |
| 56 | 56 |
|
| 57 | 57 |
std::cout << "A digraph is read from 'digraph.lgf'." << std::endl; |
| 58 | 58 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
| 59 | 59 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
| 60 | 60 |
|
| 61 | 61 |
std::cout << "We can write it to the standard output:" << std::endl; |
| 62 | 62 |
|
| 63 |
digraphWriter( |
|
| 63 |
digraphWriter(g). // write g to the standard output |
|
| 64 | 64 |
arcMap("capacity", cap). // write cap into 'capacity'
|
| 65 | 65 |
node("source", s). // write s to 'source'
|
| 66 | 66 |
node("target", t). // write t to 'target'
|
| 67 | 67 |
run(); |
| 68 | 68 |
|
| 69 | 69 |
return 0; |
| 70 | 70 |
} |
| ... | ... |
@@ -201,547 +201,548 @@ |
| 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 |
DigraphReader<Digraph> digraphReader( |
|
| 393 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 394 |
std::istream& is = std::cin); |
|
| 394 | 395 |
|
| 395 | 396 |
template <typename Digraph> |
| 396 |
DigraphReader<Digraph> digraphReader(const std::string& fn |
|
| 397 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const std::string& fn); |
|
| 397 | 398 |
|
| 398 | 399 |
template <typename Digraph> |
| 399 |
DigraphReader<Digraph> digraphReader(const char *fn |
|
| 400 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char *fn); |
|
| 400 | 401 |
|
| 401 | 402 |
/// \ingroup lemon_io |
| 402 | 403 |
/// |
| 403 | 404 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
| 404 | 405 |
/// |
| 405 | 406 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 406 | 407 |
/// |
| 407 | 408 |
/// The reading method does a batch processing. The user creates a |
| 408 | 409 |
/// reader object, then various reading rules can be added to the |
| 409 | 410 |
/// reader, and eventually the reading is executed with the \c run() |
| 410 | 411 |
/// member function. A map reading rule can be added to the reader |
| 411 | 412 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 412 | 413 |
/// converter parameter can also be added as a standard functor |
| 413 | 414 |
/// converting from \c std::string to the value type of the map. If it |
| 414 | 415 |
/// is set, it will determine how the tokens in the file should be |
| 415 | 416 |
/// converted to the value type of the map. If the functor is not set, |
| 416 | 417 |
/// then a default conversion will be used. One map can be read into |
| 417 | 418 |
/// multiple map objects at the same time. The \c attribute(), \c |
| 418 | 419 |
/// node() and \c arc() functions are used to add attribute reading |
| 419 | 420 |
/// rules. |
| 420 | 421 |
/// |
| 421 | 422 |
///\code |
| 422 |
/// DigraphReader<Digraph>(std::cin |
|
| 423 |
/// DigraphReader<Digraph>(digraph, std::cin). |
|
| 423 | 424 |
/// nodeMap("coordinates", coord_map).
|
| 424 | 425 |
/// arcMap("capacity", cap_map).
|
| 425 | 426 |
/// node("source", src).
|
| 426 | 427 |
/// node("target", trg).
|
| 427 | 428 |
/// attribute("caption", caption).
|
| 428 | 429 |
/// run(); |
| 429 | 430 |
///\endcode |
| 430 | 431 |
/// |
| 431 | 432 |
/// By default the reader uses the first section in the file of the |
| 432 | 433 |
/// proper type. If a section has an optional name, then it can be |
| 433 | 434 |
/// selected for reading by giving an optional name parameter to the |
| 434 | 435 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
| 435 | 436 |
/// |
| 436 | 437 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
| 437 | 438 |
/// that the nodes or arcs should not be constructed (added to the |
| 438 | 439 |
/// graph) during the reading, but instead the label map of the items |
| 439 | 440 |
/// are given as a parameter of these functions. An |
| 440 | 441 |
/// application of these functions is multipass reading, which is |
| 441 | 442 |
/// important if two \c \@arcs sections must be read from the |
| 442 | 443 |
/// file. In this case the first phase would read the node set and one |
| 443 | 444 |
/// of the arc sets, while the second phase would read the second arc |
| 444 | 445 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
| 445 | 446 |
/// The previously read label node map should be passed to the \c |
| 446 | 447 |
/// useNodes() functions. Another application of multipass reading when |
| 447 | 448 |
/// paths are given as a node map or an arc map. |
| 448 | 449 |
/// It is impossible to read this in |
| 449 | 450 |
/// a single pass, because the arcs are not constructed when the node |
| 450 | 451 |
/// maps are read. |
| 451 | 452 |
template <typename _Digraph> |
| 452 | 453 |
class DigraphReader {
|
| 453 | 454 |
public: |
| 454 | 455 |
|
| 455 | 456 |
typedef _Digraph Digraph; |
| 456 | 457 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 457 | 458 |
|
| 458 | 459 |
private: |
| 459 | 460 |
|
| 460 | 461 |
|
| 461 | 462 |
std::istream* _is; |
| 462 | 463 |
bool local_is; |
| 463 | 464 |
|
| 464 | 465 |
Digraph& _digraph; |
| 465 | 466 |
|
| 466 | 467 |
std::string _nodes_caption; |
| 467 | 468 |
std::string _arcs_caption; |
| 468 | 469 |
std::string _attributes_caption; |
| 469 | 470 |
|
| 470 | 471 |
typedef std::map<std::string, Node> NodeIndex; |
| 471 | 472 |
NodeIndex _node_index; |
| 472 | 473 |
typedef std::map<std::string, Arc> ArcIndex; |
| 473 | 474 |
ArcIndex _arc_index; |
| 474 | 475 |
|
| 475 | 476 |
typedef std::vector<std::pair<std::string, |
| 476 | 477 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 477 | 478 |
NodeMaps _node_maps; |
| 478 | 479 |
|
| 479 | 480 |
typedef std::vector<std::pair<std::string, |
| 480 | 481 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps; |
| 481 | 482 |
ArcMaps _arc_maps; |
| 482 | 483 |
|
| 483 | 484 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 484 | 485 |
Attributes; |
| 485 | 486 |
Attributes _attributes; |
| 486 | 487 |
|
| 487 | 488 |
bool _use_nodes; |
| 488 | 489 |
bool _use_arcs; |
| 489 | 490 |
|
| 490 | 491 |
bool _skip_nodes; |
| 491 | 492 |
bool _skip_arcs; |
| 492 | 493 |
|
| 493 | 494 |
int line_num; |
| 494 | 495 |
std::istringstream line; |
| 495 | 496 |
|
| 496 | 497 |
public: |
| 497 | 498 |
|
| 498 | 499 |
/// \brief Constructor |
| 499 | 500 |
/// |
| 500 | 501 |
/// Construct a directed graph reader, which reads from the given |
| 501 | 502 |
/// input stream. |
| 502 |
DigraphReader(std::istream& is |
|
| 503 |
DigraphReader(Digraph& digraph, std::istream& is = std::cin) |
|
| 503 | 504 |
: _is(&is), local_is(false), _digraph(digraph), |
| 504 | 505 |
_use_nodes(false), _use_arcs(false), |
| 505 | 506 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 506 | 507 |
|
| 507 | 508 |
/// \brief Constructor |
| 508 | 509 |
/// |
| 509 | 510 |
/// Construct a directed graph reader, which reads from the given |
| 510 | 511 |
/// file. |
| 511 |
DigraphReader(const std::string& fn |
|
| 512 |
DigraphReader(Digraph& digraph, const std::string& fn) |
|
| 512 | 513 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph), |
| 513 | 514 |
_use_nodes(false), _use_arcs(false), |
| 514 | 515 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 515 | 516 |
|
| 516 | 517 |
/// \brief Constructor |
| 517 | 518 |
/// |
| 518 | 519 |
/// Construct a directed graph reader, which reads from the given |
| 519 | 520 |
/// file. |
| 520 |
DigraphReader(const char* fn |
|
| 521 |
DigraphReader(Digraph& digraph, const char* fn) |
|
| 521 | 522 |
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph), |
| 522 | 523 |
_use_nodes(false), _use_arcs(false), |
| 523 | 524 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 524 | 525 |
|
| 525 | 526 |
/// \brief Destructor |
| 526 | 527 |
~DigraphReader() {
|
| 527 | 528 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 528 | 529 |
it != _node_maps.end(); ++it) {
|
| 529 | 530 |
delete it->second; |
| 530 | 531 |
} |
| 531 | 532 |
|
| 532 | 533 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 533 | 534 |
it != _arc_maps.end(); ++it) {
|
| 534 | 535 |
delete it->second; |
| 535 | 536 |
} |
| 536 | 537 |
|
| 537 | 538 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 538 | 539 |
it != _attributes.end(); ++it) {
|
| 539 | 540 |
delete it->second; |
| 540 | 541 |
} |
| 541 | 542 |
|
| 542 | 543 |
if (local_is) {
|
| 543 | 544 |
delete _is; |
| 544 | 545 |
} |
| 545 | 546 |
|
| 546 | 547 |
} |
| 547 | 548 |
|
| 548 | 549 |
private: |
| 549 | 550 |
|
| 550 |
friend DigraphReader<Digraph> digraphReader<>(std::istream& is, |
|
| 551 |
Digraph& digraph); |
|
| 552 |
friend DigraphReader<Digraph> digraphReader<>(const std::string& fn, |
|
| 553 |
Digraph& digraph); |
|
| 554 |
friend DigraphReader<Digraph> digraphReader<>(const char *fn, |
|
| 555 |
Digraph& digraph); |
|
| 551 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
|
| 552 |
std::istream& is); |
|
| 553 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
|
| 554 |
const std::string& fn); |
|
| 555 |
friend DigraphReader<Digraph> digraphReader<>(Digraph& digraph, |
|
| 556 |
const char *fn); |
|
| 556 | 557 |
|
| 557 | 558 |
DigraphReader(DigraphReader& other) |
| 558 | 559 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph), |
| 559 | 560 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs), |
| 560 | 561 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 561 | 562 |
|
| 562 | 563 |
other._is = 0; |
| 563 | 564 |
other.local_is = false; |
| 564 | 565 |
|
| 565 | 566 |
_node_index.swap(other._node_index); |
| 566 | 567 |
_arc_index.swap(other._arc_index); |
| 567 | 568 |
|
| 568 | 569 |
_node_maps.swap(other._node_maps); |
| 569 | 570 |
_arc_maps.swap(other._arc_maps); |
| 570 | 571 |
_attributes.swap(other._attributes); |
| 571 | 572 |
|
| 572 | 573 |
_nodes_caption = other._nodes_caption; |
| 573 | 574 |
_arcs_caption = other._arcs_caption; |
| 574 | 575 |
_attributes_caption = other._attributes_caption; |
| 575 | 576 |
|
| 576 | 577 |
} |
| 577 | 578 |
|
| 578 | 579 |
DigraphReader& operator=(const DigraphReader&); |
| 579 | 580 |
|
| 580 | 581 |
public: |
| 581 | 582 |
|
| 582 | 583 |
/// \name Reading rules |
| 583 | 584 |
/// @{
|
| 584 | 585 |
|
| 585 | 586 |
/// \brief Node map reading rule |
| 586 | 587 |
/// |
| 587 | 588 |
/// Add a node map reading rule to the reader. |
| 588 | 589 |
template <typename Map> |
| 589 | 590 |
DigraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 590 | 591 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 591 | 592 |
_reader_bits::MapStorageBase<Node>* storage = |
| 592 | 593 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 593 | 594 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 594 | 595 |
return *this; |
| 595 | 596 |
} |
| 596 | 597 |
|
| 597 | 598 |
/// \brief Node map reading rule |
| 598 | 599 |
/// |
| 599 | 600 |
/// Add a node map reading rule with specialized converter to the |
| 600 | 601 |
/// reader. |
| 601 | 602 |
template <typename Map, typename Converter> |
| 602 | 603 |
DigraphReader& nodeMap(const std::string& caption, Map& map, |
| 603 | 604 |
const Converter& converter = Converter()) {
|
| 604 | 605 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 605 | 606 |
_reader_bits::MapStorageBase<Node>* storage = |
| 606 | 607 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 607 | 608 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 608 | 609 |
return *this; |
| 609 | 610 |
} |
| 610 | 611 |
|
| 611 | 612 |
/// \brief Arc map reading rule |
| 612 | 613 |
/// |
| 613 | 614 |
/// Add an arc map reading rule to the reader. |
| 614 | 615 |
template <typename Map> |
| 615 | 616 |
DigraphReader& arcMap(const std::string& caption, Map& map) {
|
| 616 | 617 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 617 | 618 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 618 | 619 |
new _reader_bits::MapStorage<Arc, Map>(map); |
| 619 | 620 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 620 | 621 |
return *this; |
| 621 | 622 |
} |
| 622 | 623 |
|
| 623 | 624 |
/// \brief Arc map reading rule |
| 624 | 625 |
/// |
| 625 | 626 |
/// Add an arc map reading rule with specialized converter to the |
| 626 | 627 |
/// reader. |
| 627 | 628 |
template <typename Map, typename Converter> |
| 628 | 629 |
DigraphReader& arcMap(const std::string& caption, Map& map, |
| 629 | 630 |
const Converter& converter = Converter()) {
|
| 630 | 631 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 631 | 632 |
_reader_bits::MapStorageBase<Arc>* storage = |
| 632 | 633 |
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 633 | 634 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 634 | 635 |
return *this; |
| 635 | 636 |
} |
| 636 | 637 |
|
| 637 | 638 |
/// \brief Attribute reading rule |
| 638 | 639 |
/// |
| 639 | 640 |
/// Add an attribute reading rule to the reader. |
| 640 | 641 |
template <typename Value> |
| 641 | 642 |
DigraphReader& attribute(const std::string& caption, Value& value) {
|
| 642 | 643 |
_reader_bits::ValueStorageBase* storage = |
| 643 | 644 |
new _reader_bits::ValueStorage<Value>(value); |
| 644 | 645 |
_attributes.insert(std::make_pair(caption, storage)); |
| 645 | 646 |
return *this; |
| 646 | 647 |
} |
| 647 | 648 |
|
| 648 | 649 |
/// \brief Attribute reading rule |
| 649 | 650 |
/// |
| 650 | 651 |
/// Add an attribute reading rule with specialized converter to the |
| 651 | 652 |
/// reader. |
| 652 | 653 |
template <typename Value, typename Converter> |
| 653 | 654 |
DigraphReader& attribute(const std::string& caption, Value& value, |
| 654 | 655 |
const Converter& converter = Converter()) {
|
| 655 | 656 |
_reader_bits::ValueStorageBase* storage = |
| 656 | 657 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 657 | 658 |
_attributes.insert(std::make_pair(caption, storage)); |
| 658 | 659 |
return *this; |
| 659 | 660 |
} |
| 660 | 661 |
|
| 661 | 662 |
/// \brief Node reading rule |
| 662 | 663 |
/// |
| 663 | 664 |
/// Add a node reading rule to reader. |
| 664 | 665 |
DigraphReader& node(const std::string& caption, Node& node) {
|
| 665 | 666 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 666 | 667 |
Converter converter(_node_index); |
| 667 | 668 |
_reader_bits::ValueStorageBase* storage = |
| 668 | 669 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 669 | 670 |
_attributes.insert(std::make_pair(caption, storage)); |
| 670 | 671 |
return *this; |
| 671 | 672 |
} |
| 672 | 673 |
|
| 673 | 674 |
/// \brief Arc reading rule |
| 674 | 675 |
/// |
| 675 | 676 |
/// Add an arc reading rule to reader. |
| 676 | 677 |
DigraphReader& arc(const std::string& caption, Arc& arc) {
|
| 677 | 678 |
typedef _reader_bits::MapLookUpConverter<Arc> Converter; |
| 678 | 679 |
Converter converter(_arc_index); |
| 679 | 680 |
_reader_bits::ValueStorageBase* storage = |
| 680 | 681 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 681 | 682 |
_attributes.insert(std::make_pair(caption, storage)); |
| 682 | 683 |
return *this; |
| 683 | 684 |
} |
| 684 | 685 |
|
| 685 | 686 |
/// @} |
| 686 | 687 |
|
| 687 | 688 |
/// \name Select section by name |
| 688 | 689 |
/// @{
|
| 689 | 690 |
|
| 690 | 691 |
/// \brief Set \c \@nodes section to be read |
| 691 | 692 |
/// |
| 692 | 693 |
/// Set \c \@nodes section to be read |
| 693 | 694 |
DigraphReader& nodes(const std::string& caption) {
|
| 694 | 695 |
_nodes_caption = caption; |
| 695 | 696 |
return *this; |
| 696 | 697 |
} |
| 697 | 698 |
|
| 698 | 699 |
/// \brief Set \c \@arcs section to be read |
| 699 | 700 |
/// |
| 700 | 701 |
/// Set \c \@arcs section to be read |
| 701 | 702 |
DigraphReader& arcs(const std::string& caption) {
|
| 702 | 703 |
_arcs_caption = caption; |
| 703 | 704 |
return *this; |
| 704 | 705 |
} |
| 705 | 706 |
|
| 706 | 707 |
/// \brief Set \c \@attributes section to be read |
| 707 | 708 |
/// |
| 708 | 709 |
/// Set \c \@attributes section to be read |
| 709 | 710 |
DigraphReader& attributes(const std::string& caption) {
|
| 710 | 711 |
_attributes_caption = caption; |
| 711 | 712 |
return *this; |
| 712 | 713 |
} |
| 713 | 714 |
|
| 714 | 715 |
/// @} |
| 715 | 716 |
|
| 716 | 717 |
/// \name Using previously constructed node or arc set |
| 717 | 718 |
/// @{
|
| 718 | 719 |
|
| 719 | 720 |
/// \brief Use previously constructed node set |
| 720 | 721 |
/// |
| 721 | 722 |
/// Use previously constructed node set, and specify the node |
| 722 | 723 |
/// label map. |
| 723 | 724 |
template <typename Map> |
| 724 | 725 |
DigraphReader& useNodes(const Map& map) {
|
| 725 | 726 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 726 | 727 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 727 | 728 |
_use_nodes = true; |
| 728 | 729 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
| 729 | 730 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 730 | 731 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| 731 | 732 |
} |
| 732 | 733 |
return *this; |
| 733 | 734 |
} |
| 734 | 735 |
|
| 735 | 736 |
/// \brief Use previously constructed node set |
| 736 | 737 |
/// |
| 737 | 738 |
/// Use previously constructed node set, and specify the node |
| 738 | 739 |
/// label map and a functor which converts the label map values to |
| 739 | 740 |
/// \c std::string. |
| 740 | 741 |
template <typename Map, typename Converter> |
| 741 | 742 |
DigraphReader& useNodes(const Map& map, |
| 742 | 743 |
const Converter& converter = Converter()) {
|
| 743 | 744 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 744 | 745 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
| 745 | 746 |
_use_nodes = true; |
| 746 | 747 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 747 | 748 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
| ... | ... |
@@ -991,538 +992,540 @@ |
| 991 | 992 |
|
| 992 | 993 |
if (!_reader_bits::readToken(line, target_token)) |
| 993 | 994 |
throw DataFormatError("Target not found");
|
| 994 | 995 |
|
| 995 | 996 |
std::vector<std::string> tokens(map_num); |
| 996 | 997 |
for (int i = 0; i < map_num; ++i) {
|
| 997 | 998 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 998 | 999 |
std::ostringstream msg; |
| 999 | 1000 |
msg << "Column not found (" << i + 1 << ")";
|
| 1000 | 1001 |
throw DataFormatError(msg.str().c_str()); |
| 1001 | 1002 |
} |
| 1002 | 1003 |
} |
| 1003 | 1004 |
if (line >> std::ws >> c) |
| 1004 | 1005 |
throw DataFormatError("Extra character on the end of line");
|
| 1005 | 1006 |
|
| 1006 | 1007 |
Arc a; |
| 1007 | 1008 |
if (!_use_arcs) {
|
| 1008 | 1009 |
|
| 1009 | 1010 |
typename NodeIndex::iterator it; |
| 1010 | 1011 |
|
| 1011 | 1012 |
it = _node_index.find(source_token); |
| 1012 | 1013 |
if (it == _node_index.end()) {
|
| 1013 | 1014 |
std::ostringstream msg; |
| 1014 | 1015 |
msg << "Item not found: " << source_token; |
| 1015 | 1016 |
throw DataFormatError(msg.str().c_str()); |
| 1016 | 1017 |
} |
| 1017 | 1018 |
Node source = it->second; |
| 1018 | 1019 |
|
| 1019 | 1020 |
it = _node_index.find(target_token); |
| 1020 | 1021 |
if (it == _node_index.end()) {
|
| 1021 | 1022 |
std::ostringstream msg; |
| 1022 | 1023 |
msg << "Item not found: " << target_token; |
| 1023 | 1024 |
throw DataFormatError(msg.str().c_str()); |
| 1024 | 1025 |
} |
| 1025 | 1026 |
Node target = it->second; |
| 1026 | 1027 |
|
| 1027 | 1028 |
a = _digraph.addArc(source, target); |
| 1028 | 1029 |
if (label_index != -1) |
| 1029 | 1030 |
_arc_index.insert(std::make_pair(tokens[label_index], a)); |
| 1030 | 1031 |
} else {
|
| 1031 | 1032 |
if (label_index == -1) |
| 1032 | 1033 |
throw DataFormatError("Label map not found in file");
|
| 1033 | 1034 |
typename std::map<std::string, Arc>::iterator it = |
| 1034 | 1035 |
_arc_index.find(tokens[label_index]); |
| 1035 | 1036 |
if (it == _arc_index.end()) {
|
| 1036 | 1037 |
std::ostringstream msg; |
| 1037 | 1038 |
msg << "Arc with label not found: " << tokens[label_index]; |
| 1038 | 1039 |
throw DataFormatError(msg.str().c_str()); |
| 1039 | 1040 |
} |
| 1040 | 1041 |
a = it->second; |
| 1041 | 1042 |
} |
| 1042 | 1043 |
|
| 1043 | 1044 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
| 1044 | 1045 |
_arc_maps[i].second->set(a, tokens[map_index[i]]); |
| 1045 | 1046 |
} |
| 1046 | 1047 |
|
| 1047 | 1048 |
} |
| 1048 | 1049 |
if (readSuccess()) {
|
| 1049 | 1050 |
line.putback(c); |
| 1050 | 1051 |
} |
| 1051 | 1052 |
} |
| 1052 | 1053 |
|
| 1053 | 1054 |
void readAttributes() {
|
| 1054 | 1055 |
|
| 1055 | 1056 |
std::set<std::string> read_attr; |
| 1056 | 1057 |
|
| 1057 | 1058 |
char c; |
| 1058 | 1059 |
while (readLine() && line >> c && c != '@') {
|
| 1059 | 1060 |
line.putback(c); |
| 1060 | 1061 |
|
| 1061 | 1062 |
std::string attr, token; |
| 1062 | 1063 |
if (!_reader_bits::readToken(line, attr)) |
| 1063 | 1064 |
throw DataFormatError("Attribute name not found");
|
| 1064 | 1065 |
if (!_reader_bits::readToken(line, token)) |
| 1065 | 1066 |
throw DataFormatError("Attribute value not found");
|
| 1066 | 1067 |
if (line >> c) |
| 1067 | 1068 |
throw DataFormatError("Extra character on the end of line");
|
| 1068 | 1069 |
|
| 1069 | 1070 |
{
|
| 1070 | 1071 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1071 | 1072 |
if (it != read_attr.end()) {
|
| 1072 | 1073 |
std::ostringstream msg; |
| 1073 | 1074 |
msg << "Multiple occurence of attribute " << attr; |
| 1074 | 1075 |
throw DataFormatError(msg.str().c_str()); |
| 1075 | 1076 |
} |
| 1076 | 1077 |
read_attr.insert(attr); |
| 1077 | 1078 |
} |
| 1078 | 1079 |
|
| 1079 | 1080 |
{
|
| 1080 | 1081 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1081 | 1082 |
while (it != _attributes.end() && it->first == attr) {
|
| 1082 | 1083 |
it->second->set(token); |
| 1083 | 1084 |
++it; |
| 1084 | 1085 |
} |
| 1085 | 1086 |
} |
| 1086 | 1087 |
|
| 1087 | 1088 |
} |
| 1088 | 1089 |
if (readSuccess()) {
|
| 1089 | 1090 |
line.putback(c); |
| 1090 | 1091 |
} |
| 1091 | 1092 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1092 | 1093 |
it != _attributes.end(); ++it) {
|
| 1093 | 1094 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1094 | 1095 |
std::ostringstream msg; |
| 1095 | 1096 |
msg << "Attribute not found in file: " << it->first; |
| 1096 | 1097 |
throw DataFormatError(msg.str().c_str()); |
| 1097 | 1098 |
} |
| 1098 | 1099 |
} |
| 1099 | 1100 |
} |
| 1100 | 1101 |
|
| 1101 | 1102 |
public: |
| 1102 | 1103 |
|
| 1103 | 1104 |
/// \name Execution of the reader |
| 1104 | 1105 |
/// @{
|
| 1105 | 1106 |
|
| 1106 | 1107 |
/// \brief Start the batch processing |
| 1107 | 1108 |
/// |
| 1108 | 1109 |
/// This function starts the batch processing |
| 1109 | 1110 |
void run() {
|
| 1110 | 1111 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1111 | 1112 |
if (!*_is) {
|
| 1112 | 1113 |
throw DataFormatError("Cannot find file");
|
| 1113 | 1114 |
} |
| 1114 | 1115 |
|
| 1115 | 1116 |
bool nodes_done = _skip_nodes; |
| 1116 | 1117 |
bool arcs_done = _skip_arcs; |
| 1117 | 1118 |
bool attributes_done = false; |
| 1118 | 1119 |
|
| 1119 | 1120 |
line_num = 0; |
| 1120 | 1121 |
readLine(); |
| 1121 | 1122 |
skipSection(); |
| 1122 | 1123 |
|
| 1123 | 1124 |
while (readSuccess()) {
|
| 1124 | 1125 |
try {
|
| 1125 | 1126 |
char c; |
| 1126 | 1127 |
std::string section, caption; |
| 1127 | 1128 |
line >> c; |
| 1128 | 1129 |
_reader_bits::readToken(line, section); |
| 1129 | 1130 |
_reader_bits::readToken(line, caption); |
| 1130 | 1131 |
|
| 1131 | 1132 |
if (line >> c) |
| 1132 | 1133 |
throw DataFormatError("Extra character on the end of line");
|
| 1133 | 1134 |
|
| 1134 | 1135 |
if (section == "nodes" && !nodes_done) {
|
| 1135 | 1136 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1136 | 1137 |
readNodes(); |
| 1137 | 1138 |
nodes_done = true; |
| 1138 | 1139 |
} |
| 1139 | 1140 |
} else if ((section == "arcs" || section == "edges") && |
| 1140 | 1141 |
!arcs_done) {
|
| 1141 | 1142 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
| 1142 | 1143 |
readArcs(); |
| 1143 | 1144 |
arcs_done = true; |
| 1144 | 1145 |
} |
| 1145 | 1146 |
} else if (section == "attributes" && !attributes_done) {
|
| 1146 | 1147 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1147 | 1148 |
readAttributes(); |
| 1148 | 1149 |
attributes_done = true; |
| 1149 | 1150 |
} |
| 1150 | 1151 |
} else {
|
| 1151 | 1152 |
readLine(); |
| 1152 | 1153 |
skipSection(); |
| 1153 | 1154 |
} |
| 1154 | 1155 |
} catch (DataFormatError& error) {
|
| 1155 | 1156 |
error.line(line_num); |
| 1156 | 1157 |
throw; |
| 1157 | 1158 |
} |
| 1158 | 1159 |
} |
| 1159 | 1160 |
|
| 1160 | 1161 |
if (!nodes_done) {
|
| 1161 | 1162 |
throw DataFormatError("Section @nodes not found");
|
| 1162 | 1163 |
} |
| 1163 | 1164 |
|
| 1164 | 1165 |
if (!arcs_done) {
|
| 1165 | 1166 |
throw DataFormatError("Section @arcs not found");
|
| 1166 | 1167 |
} |
| 1167 | 1168 |
|
| 1168 | 1169 |
if (!attributes_done && !_attributes.empty()) {
|
| 1169 | 1170 |
throw DataFormatError("Section @attributes not found");
|
| 1170 | 1171 |
} |
| 1171 | 1172 |
|
| 1172 | 1173 |
} |
| 1173 | 1174 |
|
| 1174 | 1175 |
/// @} |
| 1175 | 1176 |
|
| 1176 | 1177 |
}; |
| 1177 | 1178 |
|
| 1178 | 1179 |
/// \brief Return a \ref DigraphReader class |
| 1179 | 1180 |
/// |
| 1180 | 1181 |
/// This function just returns a \ref DigraphReader class. |
| 1181 | 1182 |
/// \relates DigraphReader |
| 1182 | 1183 |
template <typename Digraph> |
| 1183 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
|
|
| 1184 |
DigraphReader<Digraph> tmp(is, digraph); |
|
| 1184 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 1185 |
std::istream& is = std::cin) {
|
|
| 1186 |
DigraphReader<Digraph> tmp(digraph, is); |
|
| 1185 | 1187 |
return tmp; |
| 1186 | 1188 |
} |
| 1187 | 1189 |
|
| 1188 | 1190 |
/// \brief Return a \ref DigraphReader class |
| 1189 | 1191 |
/// |
| 1190 | 1192 |
/// This function just returns a \ref DigraphReader class. |
| 1191 | 1193 |
/// \relates DigraphReader |
| 1192 | 1194 |
template <typename Digraph> |
| 1193 |
DigraphReader<Digraph> digraphReader(const std::string& fn, |
|
| 1194 |
Digraph& digraph) {
|
|
| 1195 |
|
|
| 1195 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, |
|
| 1196 |
const std::string& fn) {
|
|
| 1197 |
DigraphReader<Digraph> tmp(digraph, fn); |
|
| 1196 | 1198 |
return tmp; |
| 1197 | 1199 |
} |
| 1198 | 1200 |
|
| 1199 | 1201 |
/// \brief Return a \ref DigraphReader class |
| 1200 | 1202 |
/// |
| 1201 | 1203 |
/// This function just returns a \ref DigraphReader class. |
| 1202 | 1204 |
/// \relates DigraphReader |
| 1203 | 1205 |
template <typename Digraph> |
| 1204 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
|
|
| 1205 |
DigraphReader<Digraph> tmp(fn, digraph); |
|
| 1206 |
DigraphReader<Digraph> digraphReader(Digraph& digraph, const char* fn) {
|
|
| 1207 |
DigraphReader<Digraph> tmp(digraph, fn); |
|
| 1206 | 1208 |
return tmp; |
| 1207 | 1209 |
} |
| 1208 | 1210 |
|
| 1209 | 1211 |
template <typename Graph> |
| 1210 | 1212 |
class GraphReader; |
| 1211 | 1213 |
|
| 1212 | 1214 |
template <typename Graph> |
| 1213 |
GraphReader<Graph> graphReader( |
|
| 1215 |
GraphReader<Graph> graphReader(Graph& graph, |
|
| 1216 |
std::istream& is = std::cin); |
|
| 1214 | 1217 |
|
| 1215 | 1218 |
template <typename Graph> |
| 1216 |
GraphReader<Graph> graphReader(const std::string& fn |
|
| 1219 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn); |
|
| 1217 | 1220 |
|
| 1218 | 1221 |
template <typename Graph> |
| 1219 |
GraphReader<Graph> graphReader(const char *fn |
|
| 1222 |
GraphReader<Graph> graphReader(Graph& graph, const char *fn); |
|
| 1220 | 1223 |
|
| 1221 | 1224 |
/// \ingroup lemon_io |
| 1222 | 1225 |
/// |
| 1223 | 1226 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
| 1224 | 1227 |
/// |
| 1225 | 1228 |
/// This utility reads an \ref lgf-format "LGF" file. |
| 1226 | 1229 |
/// |
| 1227 | 1230 |
/// It can be used almost the same way as \c DigraphReader. |
| 1228 | 1231 |
/// The only difference is that this class can handle edges and |
| 1229 | 1232 |
/// edge maps as well as arcs and arc maps. |
| 1230 | 1233 |
/// |
| 1231 | 1234 |
/// The columns in the \c \@edges (or \c \@arcs) section are the |
| 1232 | 1235 |
/// edge maps. However, if there are two maps with the same name |
| 1233 | 1236 |
/// prefixed with \c '+' and \c '-', then these can be read into an |
| 1234 | 1237 |
/// arc map. Similarly, an attribute can be read into an arc, if |
| 1235 | 1238 |
/// it's value is an edge label prefixed with \c '+' or \c '-'. |
| 1236 | 1239 |
template <typename _Graph> |
| 1237 | 1240 |
class GraphReader {
|
| 1238 | 1241 |
public: |
| 1239 | 1242 |
|
| 1240 | 1243 |
typedef _Graph Graph; |
| 1241 | 1244 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 1242 | 1245 |
|
| 1243 | 1246 |
private: |
| 1244 | 1247 |
|
| 1245 | 1248 |
std::istream* _is; |
| 1246 | 1249 |
bool local_is; |
| 1247 | 1250 |
|
| 1248 | 1251 |
Graph& _graph; |
| 1249 | 1252 |
|
| 1250 | 1253 |
std::string _nodes_caption; |
| 1251 | 1254 |
std::string _edges_caption; |
| 1252 | 1255 |
std::string _attributes_caption; |
| 1253 | 1256 |
|
| 1254 | 1257 |
typedef std::map<std::string, Node> NodeIndex; |
| 1255 | 1258 |
NodeIndex _node_index; |
| 1256 | 1259 |
typedef std::map<std::string, Edge> EdgeIndex; |
| 1257 | 1260 |
EdgeIndex _edge_index; |
| 1258 | 1261 |
|
| 1259 | 1262 |
typedef std::vector<std::pair<std::string, |
| 1260 | 1263 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps; |
| 1261 | 1264 |
NodeMaps _node_maps; |
| 1262 | 1265 |
|
| 1263 | 1266 |
typedef std::vector<std::pair<std::string, |
| 1264 | 1267 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps; |
| 1265 | 1268 |
EdgeMaps _edge_maps; |
| 1266 | 1269 |
|
| 1267 | 1270 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> |
| 1268 | 1271 |
Attributes; |
| 1269 | 1272 |
Attributes _attributes; |
| 1270 | 1273 |
|
| 1271 | 1274 |
bool _use_nodes; |
| 1272 | 1275 |
bool _use_edges; |
| 1273 | 1276 |
|
| 1274 | 1277 |
bool _skip_nodes; |
| 1275 | 1278 |
bool _skip_edges; |
| 1276 | 1279 |
|
| 1277 | 1280 |
int line_num; |
| 1278 | 1281 |
std::istringstream line; |
| 1279 | 1282 |
|
| 1280 | 1283 |
public: |
| 1281 | 1284 |
|
| 1282 | 1285 |
/// \brief Constructor |
| 1283 | 1286 |
/// |
| 1284 | 1287 |
/// Construct an undirected graph reader, which reads from the given |
| 1285 | 1288 |
/// input stream. |
| 1286 |
GraphReader(std::istream& is |
|
| 1289 |
GraphReader(Graph& graph, std::istream& is = std::cin) |
|
| 1287 | 1290 |
: _is(&is), local_is(false), _graph(graph), |
| 1288 | 1291 |
_use_nodes(false), _use_edges(false), |
| 1289 | 1292 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1290 | 1293 |
|
| 1291 | 1294 |
/// \brief Constructor |
| 1292 | 1295 |
/// |
| 1293 | 1296 |
/// Construct an undirected graph reader, which reads from the given |
| 1294 | 1297 |
/// file. |
| 1295 |
GraphReader(const std::string& fn |
|
| 1298 |
GraphReader(Graph& graph, const std::string& fn) |
|
| 1296 | 1299 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), |
| 1297 | 1300 |
_use_nodes(false), _use_edges(false), |
| 1298 | 1301 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1299 | 1302 |
|
| 1300 | 1303 |
/// \brief Constructor |
| 1301 | 1304 |
/// |
| 1302 | 1305 |
/// Construct an undirected graph reader, which reads from the given |
| 1303 | 1306 |
/// file. |
| 1304 |
GraphReader(const char* fn |
|
| 1307 |
GraphReader(Graph& graph, const char* fn) |
|
| 1305 | 1308 |
: _is(new std::ifstream(fn)), local_is(true), _graph(graph), |
| 1306 | 1309 |
_use_nodes(false), _use_edges(false), |
| 1307 | 1310 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1308 | 1311 |
|
| 1309 | 1312 |
/// \brief Destructor |
| 1310 | 1313 |
~GraphReader() {
|
| 1311 | 1314 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1312 | 1315 |
it != _node_maps.end(); ++it) {
|
| 1313 | 1316 |
delete it->second; |
| 1314 | 1317 |
} |
| 1315 | 1318 |
|
| 1316 | 1319 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1317 | 1320 |
it != _edge_maps.end(); ++it) {
|
| 1318 | 1321 |
delete it->second; |
| 1319 | 1322 |
} |
| 1320 | 1323 |
|
| 1321 | 1324 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1322 | 1325 |
it != _attributes.end(); ++it) {
|
| 1323 | 1326 |
delete it->second; |
| 1324 | 1327 |
} |
| 1325 | 1328 |
|
| 1326 | 1329 |
if (local_is) {
|
| 1327 | 1330 |
delete _is; |
| 1328 | 1331 |
} |
| 1329 | 1332 |
|
| 1330 | 1333 |
} |
| 1331 | 1334 |
|
| 1332 | 1335 |
private: |
| 1333 |
friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph); |
|
| 1334 |
friend GraphReader<Graph> graphReader<>(const std::string& fn, |
|
| 1335 |
Graph& graph); |
|
| 1336 |
friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph); |
|
| 1336 |
friend GraphReader<Graph> graphReader<>(Graph& graph, std::istream& is); |
|
| 1337 |
friend GraphReader<Graph> graphReader<>(Graph& graph, |
|
| 1338 |
const std::string& fn); |
|
| 1339 |
friend GraphReader<Graph> graphReader<>(Graph& graph, const char *fn); |
|
| 1337 | 1340 |
|
| 1338 | 1341 |
GraphReader(GraphReader& other) |
| 1339 | 1342 |
: _is(other._is), local_is(other.local_is), _graph(other._graph), |
| 1340 | 1343 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges), |
| 1341 | 1344 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1342 | 1345 |
|
| 1343 | 1346 |
other._is = 0; |
| 1344 | 1347 |
other.local_is = false; |
| 1345 | 1348 |
|
| 1346 | 1349 |
_node_index.swap(other._node_index); |
| 1347 | 1350 |
_edge_index.swap(other._edge_index); |
| 1348 | 1351 |
|
| 1349 | 1352 |
_node_maps.swap(other._node_maps); |
| 1350 | 1353 |
_edge_maps.swap(other._edge_maps); |
| 1351 | 1354 |
_attributes.swap(other._attributes); |
| 1352 | 1355 |
|
| 1353 | 1356 |
_nodes_caption = other._nodes_caption; |
| 1354 | 1357 |
_edges_caption = other._edges_caption; |
| 1355 | 1358 |
_attributes_caption = other._attributes_caption; |
| 1356 | 1359 |
|
| 1357 | 1360 |
} |
| 1358 | 1361 |
|
| 1359 | 1362 |
GraphReader& operator=(const GraphReader&); |
| 1360 | 1363 |
|
| 1361 | 1364 |
public: |
| 1362 | 1365 |
|
| 1363 | 1366 |
/// \name Reading rules |
| 1364 | 1367 |
/// @{
|
| 1365 | 1368 |
|
| 1366 | 1369 |
/// \brief Node map reading rule |
| 1367 | 1370 |
/// |
| 1368 | 1371 |
/// Add a node map reading rule to the reader. |
| 1369 | 1372 |
template <typename Map> |
| 1370 | 1373 |
GraphReader& nodeMap(const std::string& caption, Map& map) {
|
| 1371 | 1374 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1372 | 1375 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1373 | 1376 |
new _reader_bits::MapStorage<Node, Map>(map); |
| 1374 | 1377 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1375 | 1378 |
return *this; |
| 1376 | 1379 |
} |
| 1377 | 1380 |
|
| 1378 | 1381 |
/// \brief Node map reading rule |
| 1379 | 1382 |
/// |
| 1380 | 1383 |
/// Add a node map reading rule with specialized converter to the |
| 1381 | 1384 |
/// reader. |
| 1382 | 1385 |
template <typename Map, typename Converter> |
| 1383 | 1386 |
GraphReader& nodeMap(const std::string& caption, Map& map, |
| 1384 | 1387 |
const Converter& converter = Converter()) {
|
| 1385 | 1388 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>(); |
| 1386 | 1389 |
_reader_bits::MapStorageBase<Node>* storage = |
| 1387 | 1390 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1388 | 1391 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1389 | 1392 |
return *this; |
| 1390 | 1393 |
} |
| 1391 | 1394 |
|
| 1392 | 1395 |
/// \brief Edge map reading rule |
| 1393 | 1396 |
/// |
| 1394 | 1397 |
/// Add an edge map reading rule to the reader. |
| 1395 | 1398 |
template <typename Map> |
| 1396 | 1399 |
GraphReader& edgeMap(const std::string& caption, Map& map) {
|
| 1397 | 1400 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1398 | 1401 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1399 | 1402 |
new _reader_bits::MapStorage<Edge, Map>(map); |
| 1400 | 1403 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1401 | 1404 |
return *this; |
| 1402 | 1405 |
} |
| 1403 | 1406 |
|
| 1404 | 1407 |
/// \brief Edge map reading rule |
| 1405 | 1408 |
/// |
| 1406 | 1409 |
/// Add an edge map reading rule with specialized converter to the |
| 1407 | 1410 |
/// reader. |
| 1408 | 1411 |
template <typename Map, typename Converter> |
| 1409 | 1412 |
GraphReader& edgeMap(const std::string& caption, Map& map, |
| 1410 | 1413 |
const Converter& converter = Converter()) {
|
| 1411 | 1414 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>(); |
| 1412 | 1415 |
_reader_bits::MapStorageBase<Edge>* storage = |
| 1413 | 1416 |
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1414 | 1417 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1415 | 1418 |
return *this; |
| 1416 | 1419 |
} |
| 1417 | 1420 |
|
| 1418 | 1421 |
/// \brief Arc map reading rule |
| 1419 | 1422 |
/// |
| 1420 | 1423 |
/// Add an arc map reading rule to the reader. |
| 1421 | 1424 |
template <typename Map> |
| 1422 | 1425 |
GraphReader& arcMap(const std::string& caption, Map& map) {
|
| 1423 | 1426 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1424 | 1427 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1425 | 1428 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1426 | 1429 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1427 | 1430 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1428 | 1431 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1429 | 1432 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1430 | 1433 |
return *this; |
| 1431 | 1434 |
} |
| 1432 | 1435 |
|
| 1433 | 1436 |
/// \brief Arc map reading rule |
| 1434 | 1437 |
/// |
| 1435 | 1438 |
/// Add an arc map reading rule with specialized converter to the |
| 1436 | 1439 |
/// reader. |
| 1437 | 1440 |
template <typename Map, typename Converter> |
| 1438 | 1441 |
GraphReader& arcMap(const std::string& caption, Map& map, |
| 1439 | 1442 |
const Converter& converter = Converter()) {
|
| 1440 | 1443 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>(); |
| 1441 | 1444 |
_reader_bits::MapStorageBase<Edge>* forward_storage = |
| 1442 | 1445 |
new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1443 | 1446 |
(_graph, map, converter); |
| 1444 | 1447 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1445 | 1448 |
_reader_bits::MapStorageBase<Edge>* backward_storage = |
| 1446 | 1449 |
new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1447 | 1450 |
(_graph, map, converter); |
| 1448 | 1451 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1449 | 1452 |
return *this; |
| 1450 | 1453 |
} |
| 1451 | 1454 |
|
| 1452 | 1455 |
/// \brief Attribute reading rule |
| 1453 | 1456 |
/// |
| 1454 | 1457 |
/// Add an attribute reading rule to the reader. |
| 1455 | 1458 |
template <typename Value> |
| 1456 | 1459 |
GraphReader& attribute(const std::string& caption, Value& value) {
|
| 1457 | 1460 |
_reader_bits::ValueStorageBase* storage = |
| 1458 | 1461 |
new _reader_bits::ValueStorage<Value>(value); |
| 1459 | 1462 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1460 | 1463 |
return *this; |
| 1461 | 1464 |
} |
| 1462 | 1465 |
|
| 1463 | 1466 |
/// \brief Attribute reading rule |
| 1464 | 1467 |
/// |
| 1465 | 1468 |
/// Add an attribute reading rule with specialized converter to the |
| 1466 | 1469 |
/// reader. |
| 1467 | 1470 |
template <typename Value, typename Converter> |
| 1468 | 1471 |
GraphReader& attribute(const std::string& caption, Value& value, |
| 1469 | 1472 |
const Converter& converter = Converter()) {
|
| 1470 | 1473 |
_reader_bits::ValueStorageBase* storage = |
| 1471 | 1474 |
new _reader_bits::ValueStorage<Value, Converter>(value, converter); |
| 1472 | 1475 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1473 | 1476 |
return *this; |
| 1474 | 1477 |
} |
| 1475 | 1478 |
|
| 1476 | 1479 |
/// \brief Node reading rule |
| 1477 | 1480 |
/// |
| 1478 | 1481 |
/// Add a node reading rule to reader. |
| 1479 | 1482 |
GraphReader& node(const std::string& caption, Node& node) {
|
| 1480 | 1483 |
typedef _reader_bits::MapLookUpConverter<Node> Converter; |
| 1481 | 1484 |
Converter converter(_node_index); |
| 1482 | 1485 |
_reader_bits::ValueStorageBase* storage = |
| 1483 | 1486 |
new _reader_bits::ValueStorage<Node, Converter>(node, converter); |
| 1484 | 1487 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1485 | 1488 |
return *this; |
| 1486 | 1489 |
} |
| 1487 | 1490 |
|
| 1488 | 1491 |
/// \brief Edge reading rule |
| 1489 | 1492 |
/// |
| 1490 | 1493 |
/// Add an edge reading rule to reader. |
| 1491 | 1494 |
GraphReader& edge(const std::string& caption, Edge& edge) {
|
| 1492 | 1495 |
typedef _reader_bits::MapLookUpConverter<Edge> Converter; |
| 1493 | 1496 |
Converter converter(_edge_index); |
| 1494 | 1497 |
_reader_bits::ValueStorageBase* storage = |
| 1495 | 1498 |
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1496 | 1499 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1497 | 1500 |
return *this; |
| 1498 | 1501 |
} |
| 1499 | 1502 |
|
| 1500 | 1503 |
/// \brief Arc reading rule |
| 1501 | 1504 |
/// |
| 1502 | 1505 |
/// Add an arc reading rule to reader. |
| 1503 | 1506 |
GraphReader& arc(const std::string& caption, Arc& arc) {
|
| 1504 | 1507 |
typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1505 | 1508 |
Converter converter(_graph, _edge_index); |
| 1506 | 1509 |
_reader_bits::ValueStorageBase* storage = |
| 1507 | 1510 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1508 | 1511 |
_attributes.insert(std::make_pair(caption, storage)); |
| 1509 | 1512 |
return *this; |
| 1510 | 1513 |
} |
| 1511 | 1514 |
|
| 1512 | 1515 |
/// @} |
| 1513 | 1516 |
|
| 1514 | 1517 |
/// \name Select section by name |
| 1515 | 1518 |
/// @{
|
| 1516 | 1519 |
|
| 1517 | 1520 |
/// \brief Set \c \@nodes section to be read |
| 1518 | 1521 |
/// |
| 1519 | 1522 |
/// Set \c \@nodes section to be read. |
| 1520 | 1523 |
GraphReader& nodes(const std::string& caption) {
|
| 1521 | 1524 |
_nodes_caption = caption; |
| 1522 | 1525 |
return *this; |
| 1523 | 1526 |
} |
| 1524 | 1527 |
|
| 1525 | 1528 |
/// \brief Set \c \@edges section to be read |
| 1526 | 1529 |
/// |
| 1527 | 1530 |
/// Set \c \@edges section to be read. |
| 1528 | 1531 |
GraphReader& edges(const std::string& caption) {
|
| ... | ... |
@@ -1817,407 +1820,406 @@ |
| 1817 | 1820 |
if (!_reader_bits::readToken(line, source_token)) |
| 1818 | 1821 |
throw DataFormatError("Node u not found");
|
| 1819 | 1822 |
|
| 1820 | 1823 |
if (!_reader_bits::readToken(line, target_token)) |
| 1821 | 1824 |
throw DataFormatError("Node v not found");
|
| 1822 | 1825 |
|
| 1823 | 1826 |
std::vector<std::string> tokens(map_num); |
| 1824 | 1827 |
for (int i = 0; i < map_num; ++i) {
|
| 1825 | 1828 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
| 1826 | 1829 |
std::ostringstream msg; |
| 1827 | 1830 |
msg << "Column not found (" << i + 1 << ")";
|
| 1828 | 1831 |
throw DataFormatError(msg.str().c_str()); |
| 1829 | 1832 |
} |
| 1830 | 1833 |
} |
| 1831 | 1834 |
if (line >> std::ws >> c) |
| 1832 | 1835 |
throw DataFormatError("Extra character on the end of line");
|
| 1833 | 1836 |
|
| 1834 | 1837 |
Edge e; |
| 1835 | 1838 |
if (!_use_edges) {
|
| 1836 | 1839 |
|
| 1837 | 1840 |
typename NodeIndex::iterator it; |
| 1838 | 1841 |
|
| 1839 | 1842 |
it = _node_index.find(source_token); |
| 1840 | 1843 |
if (it == _node_index.end()) {
|
| 1841 | 1844 |
std::ostringstream msg; |
| 1842 | 1845 |
msg << "Item not found: " << source_token; |
| 1843 | 1846 |
throw DataFormatError(msg.str().c_str()); |
| 1844 | 1847 |
} |
| 1845 | 1848 |
Node source = it->second; |
| 1846 | 1849 |
|
| 1847 | 1850 |
it = _node_index.find(target_token); |
| 1848 | 1851 |
if (it == _node_index.end()) {
|
| 1849 | 1852 |
std::ostringstream msg; |
| 1850 | 1853 |
msg << "Item not found: " << target_token; |
| 1851 | 1854 |
throw DataFormatError(msg.str().c_str()); |
| 1852 | 1855 |
} |
| 1853 | 1856 |
Node target = it->second; |
| 1854 | 1857 |
|
| 1855 | 1858 |
e = _graph.addEdge(source, target); |
| 1856 | 1859 |
if (label_index != -1) |
| 1857 | 1860 |
_edge_index.insert(std::make_pair(tokens[label_index], e)); |
| 1858 | 1861 |
} else {
|
| 1859 | 1862 |
if (label_index == -1) |
| 1860 | 1863 |
throw DataFormatError("Label map not found in file");
|
| 1861 | 1864 |
typename std::map<std::string, Edge>::iterator it = |
| 1862 | 1865 |
_edge_index.find(tokens[label_index]); |
| 1863 | 1866 |
if (it == _edge_index.end()) {
|
| 1864 | 1867 |
std::ostringstream msg; |
| 1865 | 1868 |
msg << "Edge with label not found: " << tokens[label_index]; |
| 1866 | 1869 |
throw DataFormatError(msg.str().c_str()); |
| 1867 | 1870 |
} |
| 1868 | 1871 |
e = it->second; |
| 1869 | 1872 |
} |
| 1870 | 1873 |
|
| 1871 | 1874 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
| 1872 | 1875 |
_edge_maps[i].second->set(e, tokens[map_index[i]]); |
| 1873 | 1876 |
} |
| 1874 | 1877 |
|
| 1875 | 1878 |
} |
| 1876 | 1879 |
if (readSuccess()) {
|
| 1877 | 1880 |
line.putback(c); |
| 1878 | 1881 |
} |
| 1879 | 1882 |
} |
| 1880 | 1883 |
|
| 1881 | 1884 |
void readAttributes() {
|
| 1882 | 1885 |
|
| 1883 | 1886 |
std::set<std::string> read_attr; |
| 1884 | 1887 |
|
| 1885 | 1888 |
char c; |
| 1886 | 1889 |
while (readLine() && line >> c && c != '@') {
|
| 1887 | 1890 |
line.putback(c); |
| 1888 | 1891 |
|
| 1889 | 1892 |
std::string attr, token; |
| 1890 | 1893 |
if (!_reader_bits::readToken(line, attr)) |
| 1891 | 1894 |
throw DataFormatError("Attribute name not found");
|
| 1892 | 1895 |
if (!_reader_bits::readToken(line, token)) |
| 1893 | 1896 |
throw DataFormatError("Attribute value not found");
|
| 1894 | 1897 |
if (line >> c) |
| 1895 | 1898 |
throw DataFormatError("Extra character on the end of line");
|
| 1896 | 1899 |
|
| 1897 | 1900 |
{
|
| 1898 | 1901 |
std::set<std::string>::iterator it = read_attr.find(attr); |
| 1899 | 1902 |
if (it != read_attr.end()) {
|
| 1900 | 1903 |
std::ostringstream msg; |
| 1901 | 1904 |
msg << "Multiple occurence of attribute " << attr; |
| 1902 | 1905 |
throw DataFormatError(msg.str().c_str()); |
| 1903 | 1906 |
} |
| 1904 | 1907 |
read_attr.insert(attr); |
| 1905 | 1908 |
} |
| 1906 | 1909 |
|
| 1907 | 1910 |
{
|
| 1908 | 1911 |
typename Attributes::iterator it = _attributes.lower_bound(attr); |
| 1909 | 1912 |
while (it != _attributes.end() && it->first == attr) {
|
| 1910 | 1913 |
it->second->set(token); |
| 1911 | 1914 |
++it; |
| 1912 | 1915 |
} |
| 1913 | 1916 |
} |
| 1914 | 1917 |
|
| 1915 | 1918 |
} |
| 1916 | 1919 |
if (readSuccess()) {
|
| 1917 | 1920 |
line.putback(c); |
| 1918 | 1921 |
} |
| 1919 | 1922 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1920 | 1923 |
it != _attributes.end(); ++it) {
|
| 1921 | 1924 |
if (read_attr.find(it->first) == read_attr.end()) {
|
| 1922 | 1925 |
std::ostringstream msg; |
| 1923 | 1926 |
msg << "Attribute not found in file: " << it->first; |
| 1924 | 1927 |
throw DataFormatError(msg.str().c_str()); |
| 1925 | 1928 |
} |
| 1926 | 1929 |
} |
| 1927 | 1930 |
} |
| 1928 | 1931 |
|
| 1929 | 1932 |
public: |
| 1930 | 1933 |
|
| 1931 | 1934 |
/// \name Execution of the reader |
| 1932 | 1935 |
/// @{
|
| 1933 | 1936 |
|
| 1934 | 1937 |
/// \brief Start the batch processing |
| 1935 | 1938 |
/// |
| 1936 | 1939 |
/// This function starts the batch processing |
| 1937 | 1940 |
void run() {
|
| 1938 | 1941 |
|
| 1939 | 1942 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 1940 | 1943 |
|
| 1941 | 1944 |
bool nodes_done = _skip_nodes; |
| 1942 | 1945 |
bool edges_done = _skip_edges; |
| 1943 | 1946 |
bool attributes_done = false; |
| 1944 | 1947 |
|
| 1945 | 1948 |
line_num = 0; |
| 1946 | 1949 |
readLine(); |
| 1947 | 1950 |
skipSection(); |
| 1948 | 1951 |
|
| 1949 | 1952 |
while (readSuccess()) {
|
| 1950 | 1953 |
try {
|
| 1951 | 1954 |
char c; |
| 1952 | 1955 |
std::string section, caption; |
| 1953 | 1956 |
line >> c; |
| 1954 | 1957 |
_reader_bits::readToken(line, section); |
| 1955 | 1958 |
_reader_bits::readToken(line, caption); |
| 1956 | 1959 |
|
| 1957 | 1960 |
if (line >> c) |
| 1958 | 1961 |
throw DataFormatError("Extra character on the end of line");
|
| 1959 | 1962 |
|
| 1960 | 1963 |
if (section == "nodes" && !nodes_done) {
|
| 1961 | 1964 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
| 1962 | 1965 |
readNodes(); |
| 1963 | 1966 |
nodes_done = true; |
| 1964 | 1967 |
} |
| 1965 | 1968 |
} else if ((section == "edges" || section == "arcs") && |
| 1966 | 1969 |
!edges_done) {
|
| 1967 | 1970 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
| 1968 | 1971 |
readEdges(); |
| 1969 | 1972 |
edges_done = true; |
| 1970 | 1973 |
} |
| 1971 | 1974 |
} else if (section == "attributes" && !attributes_done) {
|
| 1972 | 1975 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
| 1973 | 1976 |
readAttributes(); |
| 1974 | 1977 |
attributes_done = true; |
| 1975 | 1978 |
} |
| 1976 | 1979 |
} else {
|
| 1977 | 1980 |
readLine(); |
| 1978 | 1981 |
skipSection(); |
| 1979 | 1982 |
} |
| 1980 | 1983 |
} catch (DataFormatError& error) {
|
| 1981 | 1984 |
error.line(line_num); |
| 1982 | 1985 |
throw; |
| 1983 | 1986 |
} |
| 1984 | 1987 |
} |
| 1985 | 1988 |
|
| 1986 | 1989 |
if (!nodes_done) {
|
| 1987 | 1990 |
throw DataFormatError("Section @nodes not found");
|
| 1988 | 1991 |
} |
| 1989 | 1992 |
|
| 1990 | 1993 |
if (!edges_done) {
|
| 1991 | 1994 |
throw DataFormatError("Section @edges not found");
|
| 1992 | 1995 |
} |
| 1993 | 1996 |
|
| 1994 | 1997 |
if (!attributes_done && !_attributes.empty()) {
|
| 1995 | 1998 |
throw DataFormatError("Section @attributes not found");
|
| 1996 | 1999 |
} |
| 1997 | 2000 |
|
| 1998 | 2001 |
} |
| 1999 | 2002 |
|
| 2000 | 2003 |
/// @} |
| 2001 | 2004 |
|
| 2002 | 2005 |
}; |
| 2003 | 2006 |
|
| 2004 | 2007 |
/// \brief Return a \ref GraphReader class |
| 2005 | 2008 |
/// |
| 2006 | 2009 |
/// This function just returns a \ref GraphReader class. |
| 2007 | 2010 |
/// \relates GraphReader |
| 2008 | 2011 |
template <typename Graph> |
| 2009 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
|
|
| 2010 |
GraphReader<Graph> tmp(is, graph); |
|
| 2012 |
GraphReader<Graph> graphReader(Graph& graph, std::istream& is = std::cin) {
|
|
| 2013 |
GraphReader<Graph> tmp(graph, is); |
|
| 2011 | 2014 |
return tmp; |
| 2012 | 2015 |
} |
| 2013 | 2016 |
|
| 2014 | 2017 |
/// \brief Return a \ref GraphReader class |
| 2015 | 2018 |
/// |
| 2016 | 2019 |
/// This function just returns a \ref GraphReader class. |
| 2017 | 2020 |
/// \relates GraphReader |
| 2018 | 2021 |
template <typename Graph> |
| 2019 |
GraphReader<Graph> graphReader(const std::string& fn, |
|
| 2020 |
Graph& graph) {
|
|
| 2021 |
|
|
| 2022 |
GraphReader<Graph> graphReader(Graph& graph, const std::string& fn) {
|
|
| 2023 |
GraphReader<Graph> tmp(graph, fn); |
|
| 2022 | 2024 |
return tmp; |
| 2023 | 2025 |
} |
| 2024 | 2026 |
|
| 2025 | 2027 |
/// \brief Return a \ref GraphReader class |
| 2026 | 2028 |
/// |
| 2027 | 2029 |
/// This function just returns a \ref GraphReader class. |
| 2028 | 2030 |
/// \relates GraphReader |
| 2029 | 2031 |
template <typename Graph> |
| 2030 |
GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
|
|
| 2031 |
GraphReader<Graph> tmp(fn, graph); |
|
| 2032 |
GraphReader<Graph> graphReader(Graph& graph, const char* fn) {
|
|
| 2033 |
GraphReader<Graph> tmp(graph, fn); |
|
| 2032 | 2034 |
return tmp; |
| 2033 | 2035 |
} |
| 2034 | 2036 |
|
| 2035 | 2037 |
class SectionReader; |
| 2036 | 2038 |
|
| 2037 | 2039 |
SectionReader sectionReader(std::istream& is); |
| 2038 | 2040 |
SectionReader sectionReader(const std::string& fn); |
| 2039 | 2041 |
SectionReader sectionReader(const char* fn); |
| 2040 | 2042 |
|
| 2041 | 2043 |
/// \ingroup lemon_io |
| 2042 | 2044 |
/// |
| 2043 | 2045 |
/// \brief Section reader class |
| 2044 | 2046 |
/// |
| 2045 | 2047 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 2046 | 2048 |
/// which contain any data in arbitrary format. Such sections can be |
| 2047 | 2049 |
/// read with this class. A reading rule can be added to the class |
| 2048 | 2050 |
/// with two different functions. With the \c sectionLines() function a |
| 2049 | 2051 |
/// functor can process the section line-by-line, while with the \c |
| 2050 | 2052 |
/// sectionStream() member the section can be read from an input |
| 2051 | 2053 |
/// stream. |
| 2052 | 2054 |
class SectionReader {
|
| 2053 | 2055 |
private: |
| 2054 | 2056 |
|
| 2055 | 2057 |
std::istream* _is; |
| 2056 | 2058 |
bool local_is; |
| 2057 | 2059 |
|
| 2058 | 2060 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
| 2059 | 2061 |
Sections _sections; |
| 2060 | 2062 |
|
| 2061 | 2063 |
int line_num; |
| 2062 | 2064 |
std::istringstream line; |
| 2063 | 2065 |
|
| 2064 | 2066 |
public: |
| 2065 | 2067 |
|
| 2066 | 2068 |
/// \brief Constructor |
| 2067 | 2069 |
/// |
| 2068 | 2070 |
/// Construct a section reader, which reads from the given input |
| 2069 | 2071 |
/// stream. |
| 2070 | 2072 |
SectionReader(std::istream& is) |
| 2071 | 2073 |
: _is(&is), local_is(false) {}
|
| 2072 | 2074 |
|
| 2073 | 2075 |
/// \brief Constructor |
| 2074 | 2076 |
/// |
| 2075 | 2077 |
/// Construct a section reader, which reads from the given file. |
| 2076 | 2078 |
SectionReader(const std::string& fn) |
| 2077 | 2079 |
: _is(new std::ifstream(fn.c_str())), local_is(true) {}
|
| 2078 | 2080 |
|
| 2079 | 2081 |
/// \brief Constructor |
| 2080 | 2082 |
/// |
| 2081 | 2083 |
/// Construct a section reader, which reads from the given file. |
| 2082 | 2084 |
SectionReader(const char* fn) |
| 2083 | 2085 |
: _is(new std::ifstream(fn)), local_is(true) {}
|
| 2084 | 2086 |
|
| 2085 | 2087 |
/// \brief Destructor |
| 2086 | 2088 |
~SectionReader() {
|
| 2087 | 2089 |
for (Sections::iterator it = _sections.begin(); |
| 2088 | 2090 |
it != _sections.end(); ++it) {
|
| 2089 | 2091 |
delete it->second; |
| 2090 | 2092 |
} |
| 2091 | 2093 |
|
| 2092 | 2094 |
if (local_is) {
|
| 2093 | 2095 |
delete _is; |
| 2094 | 2096 |
} |
| 2095 | 2097 |
|
| 2096 | 2098 |
} |
| 2097 | 2099 |
|
| 2098 | 2100 |
private: |
| 2099 | 2101 |
|
| 2100 | 2102 |
friend SectionReader sectionReader(std::istream& is); |
| 2101 | 2103 |
friend SectionReader sectionReader(const std::string& fn); |
| 2102 | 2104 |
friend SectionReader sectionReader(const char* fn); |
| 2103 | 2105 |
|
| 2104 | 2106 |
SectionReader(SectionReader& other) |
| 2105 | 2107 |
: _is(other._is), local_is(other.local_is) {
|
| 2106 | 2108 |
|
| 2107 | 2109 |
other._is = 0; |
| 2108 | 2110 |
other.local_is = false; |
| 2109 | 2111 |
|
| 2110 | 2112 |
_sections.swap(other._sections); |
| 2111 | 2113 |
} |
| 2112 | 2114 |
|
| 2113 | 2115 |
SectionReader& operator=(const SectionReader&); |
| 2114 | 2116 |
|
| 2115 | 2117 |
public: |
| 2116 | 2118 |
|
| 2117 | 2119 |
/// \name Section readers |
| 2118 | 2120 |
/// @{
|
| 2119 | 2121 |
|
| 2120 | 2122 |
/// \brief Add a section processor with line oriented reading |
| 2121 | 2123 |
/// |
| 2122 | 2124 |
/// The first parameter is the type descriptor of the section, the |
| 2123 | 2125 |
/// second is a functor, which takes just one \c std::string |
| 2124 | 2126 |
/// parameter. At the reading process, each line of the section |
| 2125 | 2127 |
/// will be given to the functor object. However, the empty lines |
| 2126 | 2128 |
/// and the comment lines are filtered out, and the leading |
| 2127 | 2129 |
/// whitespaces are trimmed from each processed string. |
| 2128 | 2130 |
/// |
| 2129 | 2131 |
/// For example let's see a section, which contain several |
| 2130 | 2132 |
/// integers, which should be inserted into a vector. |
| 2131 | 2133 |
///\code |
| 2132 | 2134 |
/// @numbers |
| 2133 | 2135 |
/// 12 45 23 |
| 2134 | 2136 |
/// 4 |
| 2135 | 2137 |
/// 23 6 |
| 2136 | 2138 |
///\endcode |
| 2137 | 2139 |
/// |
| 2138 | 2140 |
/// The functor is implemented as a struct: |
| 2139 | 2141 |
///\code |
| 2140 | 2142 |
/// struct NumberSection {
|
| 2141 | 2143 |
/// std::vector<int>& _data; |
| 2142 | 2144 |
/// NumberSection(std::vector<int>& data) : _data(data) {}
|
| 2143 | 2145 |
/// void operator()(const std::string& line) {
|
| 2144 | 2146 |
/// std::istringstream ls(line); |
| 2145 | 2147 |
/// int value; |
| 2146 | 2148 |
/// while (ls >> value) _data.push_back(value); |
| 2147 | 2149 |
/// } |
| 2148 | 2150 |
/// }; |
| 2149 | 2151 |
/// |
| 2150 | 2152 |
/// // ... |
| 2151 | 2153 |
/// |
| 2152 | 2154 |
/// reader.sectionLines("numbers", NumberSection(vec));
|
| 2153 | 2155 |
///\endcode |
| 2154 | 2156 |
template <typename Functor> |
| 2155 | 2157 |
SectionReader& sectionLines(const std::string& type, Functor functor) {
|
| 2156 | 2158 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2157 | 2159 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2158 | 2160 |
"Multiple reading of section."); |
| 2159 | 2161 |
_sections.insert(std::make_pair(type, |
| 2160 | 2162 |
new _reader_bits::LineSection<Functor>(functor))); |
| 2161 | 2163 |
return *this; |
| 2162 | 2164 |
} |
| 2163 | 2165 |
|
| 2164 | 2166 |
|
| 2165 | 2167 |
/// \brief Add a section processor with stream oriented reading |
| 2166 | 2168 |
/// |
| 2167 | 2169 |
/// The first parameter is the type of the section, the second is |
| 2168 | 2170 |
/// a functor, which takes an \c std::istream& and an \c int& |
| 2169 | 2171 |
/// parameter, the latter regard to the line number of stream. The |
| 2170 | 2172 |
/// functor can read the input while the section go on, and the |
| 2171 | 2173 |
/// line number should be modified accordingly. |
| 2172 | 2174 |
template <typename Functor> |
| 2173 | 2175 |
SectionReader& sectionStream(const std::string& type, Functor functor) {
|
| 2174 | 2176 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 2175 | 2177 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
| 2176 | 2178 |
"Multiple reading of section."); |
| 2177 | 2179 |
_sections.insert(std::make_pair(type, |
| 2178 | 2180 |
new _reader_bits::StreamSection<Functor>(functor))); |
| 2179 | 2181 |
return *this; |
| 2180 | 2182 |
} |
| 2181 | 2183 |
|
| 2182 | 2184 |
/// @} |
| 2183 | 2185 |
|
| 2184 | 2186 |
private: |
| 2185 | 2187 |
|
| 2186 | 2188 |
bool readLine() {
|
| 2187 | 2189 |
std::string str; |
| 2188 | 2190 |
while(++line_num, std::getline(*_is, str)) {
|
| 2189 | 2191 |
line.clear(); line.str(str); |
| 2190 | 2192 |
char c; |
| 2191 | 2193 |
if (line >> std::ws >> c && c != '#') {
|
| 2192 | 2194 |
line.putback(c); |
| 2193 | 2195 |
return true; |
| 2194 | 2196 |
} |
| 2195 | 2197 |
} |
| 2196 | 2198 |
return false; |
| 2197 | 2199 |
} |
| 2198 | 2200 |
|
| 2199 | 2201 |
bool readSuccess() {
|
| 2200 | 2202 |
return static_cast<bool>(*_is); |
| 2201 | 2203 |
} |
| 2202 | 2204 |
|
| 2203 | 2205 |
void skipSection() {
|
| 2204 | 2206 |
char c; |
| 2205 | 2207 |
while (readSuccess() && line >> c && c != '@') {
|
| 2206 | 2208 |
readLine(); |
| 2207 | 2209 |
} |
| 2208 | 2210 |
line.putback(c); |
| 2209 | 2211 |
} |
| 2210 | 2212 |
|
| 2211 | 2213 |
public: |
| 2212 | 2214 |
|
| 2213 | 2215 |
|
| 2214 | 2216 |
/// \name Execution of the reader |
| 2215 | 2217 |
/// @{
|
| 2216 | 2218 |
|
| 2217 | 2219 |
/// \brief Start the batch processing |
| 2218 | 2220 |
/// |
| 2219 | 2221 |
/// This function starts the batch processing. |
| 2220 | 2222 |
void run() {
|
| 2221 | 2223 |
|
| 2222 | 2224 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
| 2223 | 2225 |
| ... | ... |
@@ -163,534 +163,534 @@ |
| 163 | 163 |
GraphArcMapLess<Graph, dir, Map> less(_graph, _map); |
| 164 | 164 |
std::sort(items.begin(), items.end(), less); |
| 165 | 165 |
} |
| 166 | 166 |
}; |
| 167 | 167 |
|
| 168 | 168 |
class ValueStorageBase {
|
| 169 | 169 |
public: |
| 170 | 170 |
ValueStorageBase() {}
|
| 171 | 171 |
virtual ~ValueStorageBase() {}
|
| 172 | 172 |
|
| 173 | 173 |
virtual std::string get() = 0; |
| 174 | 174 |
}; |
| 175 | 175 |
|
| 176 | 176 |
template <typename _Value, typename _Converter = DefaultConverter<_Value> > |
| 177 | 177 |
class ValueStorage : public ValueStorageBase {
|
| 178 | 178 |
public: |
| 179 | 179 |
typedef _Value Value; |
| 180 | 180 |
typedef _Converter Converter; |
| 181 | 181 |
|
| 182 | 182 |
private: |
| 183 | 183 |
const Value& _value; |
| 184 | 184 |
Converter _converter; |
| 185 | 185 |
|
| 186 | 186 |
public: |
| 187 | 187 |
ValueStorage(const Value& value, const Converter& converter = Converter()) |
| 188 | 188 |
: _value(value), _converter(converter) {}
|
| 189 | 189 |
|
| 190 | 190 |
virtual std::string get() {
|
| 191 | 191 |
return _converter(_value); |
| 192 | 192 |
} |
| 193 | 193 |
}; |
| 194 | 194 |
|
| 195 | 195 |
template <typename Value> |
| 196 | 196 |
struct MapLookUpConverter {
|
| 197 | 197 |
const std::map<Value, std::string>& _map; |
| 198 | 198 |
|
| 199 | 199 |
MapLookUpConverter(const std::map<Value, std::string>& map) |
| 200 | 200 |
: _map(map) {}
|
| 201 | 201 |
|
| 202 | 202 |
std::string operator()(const Value& str) {
|
| 203 | 203 |
typename std::map<Value, std::string>::const_iterator it = |
| 204 | 204 |
_map.find(str); |
| 205 | 205 |
if (it == _map.end()) {
|
| 206 | 206 |
throw DataFormatError("Item not found");
|
| 207 | 207 |
} |
| 208 | 208 |
return it->second; |
| 209 | 209 |
} |
| 210 | 210 |
}; |
| 211 | 211 |
|
| 212 | 212 |
template <typename Graph> |
| 213 | 213 |
struct GraphArcLookUpConverter {
|
| 214 | 214 |
const Graph& _graph; |
| 215 | 215 |
const std::map<typename Graph::Edge, std::string>& _map; |
| 216 | 216 |
|
| 217 | 217 |
GraphArcLookUpConverter(const Graph& graph, |
| 218 | 218 |
const std::map<typename Graph::Edge, |
| 219 | 219 |
std::string>& map) |
| 220 | 220 |
: _graph(graph), _map(map) {}
|
| 221 | 221 |
|
| 222 | 222 |
std::string operator()(const typename Graph::Arc& val) {
|
| 223 | 223 |
typename std::map<typename Graph::Edge, std::string> |
| 224 | 224 |
::const_iterator it = _map.find(val); |
| 225 | 225 |
if (it == _map.end()) {
|
| 226 | 226 |
throw DataFormatError("Item not found");
|
| 227 | 227 |
} |
| 228 | 228 |
return (_graph.direction(val) ? '+' : '-') + it->second; |
| 229 | 229 |
} |
| 230 | 230 |
}; |
| 231 | 231 |
|
| 232 | 232 |
inline bool isWhiteSpace(char c) {
|
| 233 | 233 |
return c == ' ' || c == '\t' || c == '\v' || |
| 234 | 234 |
c == '\n' || c == '\r' || c == '\f'; |
| 235 | 235 |
} |
| 236 | 236 |
|
| 237 | 237 |
inline bool isEscaped(char c) {
|
| 238 | 238 |
return c == '\\' || c == '\"' || c == '\'' || |
| 239 | 239 |
c == '\a' || c == '\b'; |
| 240 | 240 |
} |
| 241 | 241 |
|
| 242 | 242 |
inline static void writeEscape(std::ostream& os, char c) {
|
| 243 | 243 |
switch (c) {
|
| 244 | 244 |
case '\\': |
| 245 | 245 |
os << "\\\\"; |
| 246 | 246 |
return; |
| 247 | 247 |
case '\"': |
| 248 | 248 |
os << "\\\""; |
| 249 | 249 |
return; |
| 250 | 250 |
case '\a': |
| 251 | 251 |
os << "\\a"; |
| 252 | 252 |
return; |
| 253 | 253 |
case '\b': |
| 254 | 254 |
os << "\\b"; |
| 255 | 255 |
return; |
| 256 | 256 |
case '\f': |
| 257 | 257 |
os << "\\f"; |
| 258 | 258 |
return; |
| 259 | 259 |
case '\r': |
| 260 | 260 |
os << "\\r"; |
| 261 | 261 |
return; |
| 262 | 262 |
case '\n': |
| 263 | 263 |
os << "\\n"; |
| 264 | 264 |
return; |
| 265 | 265 |
case '\t': |
| 266 | 266 |
os << "\\t"; |
| 267 | 267 |
return; |
| 268 | 268 |
case '\v': |
| 269 | 269 |
os << "\\v"; |
| 270 | 270 |
return; |
| 271 | 271 |
default: |
| 272 | 272 |
if (c < 0x20) {
|
| 273 | 273 |
std::ios::fmtflags flags = os.flags(); |
| 274 | 274 |
os << '\\' << std::oct << static_cast<int>(c); |
| 275 | 275 |
os.flags(flags); |
| 276 | 276 |
} else {
|
| 277 | 277 |
os << c; |
| 278 | 278 |
} |
| 279 | 279 |
return; |
| 280 | 280 |
} |
| 281 | 281 |
} |
| 282 | 282 |
|
| 283 | 283 |
inline bool requireEscape(const std::string& str) {
|
| 284 | 284 |
if (str.empty() || str[0] == '@') return true; |
| 285 | 285 |
std::istringstream is(str); |
| 286 | 286 |
char c; |
| 287 | 287 |
while (is.get(c)) {
|
| 288 | 288 |
if (isWhiteSpace(c) || isEscaped(c)) {
|
| 289 | 289 |
return true; |
| 290 | 290 |
} |
| 291 | 291 |
} |
| 292 | 292 |
return false; |
| 293 | 293 |
} |
| 294 | 294 |
|
| 295 | 295 |
inline std::ostream& writeToken(std::ostream& os, const std::string& str) {
|
| 296 | 296 |
|
| 297 | 297 |
if (requireEscape(str)) {
|
| 298 | 298 |
os << '\"'; |
| 299 | 299 |
for (std::string::const_iterator it = str.begin(); |
| 300 | 300 |
it != str.end(); ++it) {
|
| 301 | 301 |
writeEscape(os, *it); |
| 302 | 302 |
} |
| 303 | 303 |
os << '\"'; |
| 304 | 304 |
} else {
|
| 305 | 305 |
os << str; |
| 306 | 306 |
} |
| 307 | 307 |
return os; |
| 308 | 308 |
} |
| 309 | 309 |
|
| 310 | 310 |
class Section {
|
| 311 | 311 |
public: |
| 312 | 312 |
virtual ~Section() {}
|
| 313 | 313 |
virtual void process(std::ostream& os) = 0; |
| 314 | 314 |
}; |
| 315 | 315 |
|
| 316 | 316 |
template <typename Functor> |
| 317 | 317 |
class LineSection : public Section {
|
| 318 | 318 |
private: |
| 319 | 319 |
|
| 320 | 320 |
Functor _functor; |
| 321 | 321 |
|
| 322 | 322 |
public: |
| 323 | 323 |
|
| 324 | 324 |
LineSection(const Functor& functor) : _functor(functor) {}
|
| 325 | 325 |
virtual ~LineSection() {}
|
| 326 | 326 |
|
| 327 | 327 |
virtual void process(std::ostream& os) {
|
| 328 | 328 |
std::string line; |
| 329 | 329 |
while (!(line = _functor()).empty()) os << line << std::endl; |
| 330 | 330 |
} |
| 331 | 331 |
}; |
| 332 | 332 |
|
| 333 | 333 |
template <typename Functor> |
| 334 | 334 |
class StreamSection : public Section {
|
| 335 | 335 |
private: |
| 336 | 336 |
|
| 337 | 337 |
Functor _functor; |
| 338 | 338 |
|
| 339 | 339 |
public: |
| 340 | 340 |
|
| 341 | 341 |
StreamSection(const Functor& functor) : _functor(functor) {}
|
| 342 | 342 |
virtual ~StreamSection() {}
|
| 343 | 343 |
|
| 344 | 344 |
virtual void process(std::ostream& os) {
|
| 345 | 345 |
_functor(os); |
| 346 | 346 |
} |
| 347 | 347 |
}; |
| 348 | 348 |
|
| 349 | 349 |
} |
| 350 | 350 |
|
| 351 | 351 |
template <typename Digraph> |
| 352 | 352 |
class DigraphWriter; |
| 353 | 353 |
|
| 354 | 354 |
template <typename Digraph> |
| 355 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os, |
|
| 356 |
const Digraph& digraph); |
|
| 355 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 356 |
std::ostream& os = std::cout); |
|
| 357 | 357 |
|
| 358 | 358 |
template <typename Digraph> |
| 359 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
|
| 360 |
const Digraph& digraph); |
|
| 359 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 360 |
const std::string& fn); |
|
| 361 | 361 |
|
| 362 | 362 |
template <typename Digraph> |
| 363 |
DigraphWriter<Digraph> digraphWriter(const char *fn, |
|
| 364 |
const Digraph& digraph); |
|
| 363 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 364 |
const char *fn); |
|
| 365 | 365 |
|
| 366 | 366 |
/// \ingroup lemon_io |
| 367 | 367 |
/// |
| 368 | 368 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 369 | 369 |
/// |
| 370 | 370 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 371 | 371 |
/// |
| 372 | 372 |
/// The writing method does a batch processing. The user creates a |
| 373 | 373 |
/// writer object, then various writing rules can be added to the |
| 374 | 374 |
/// writer, and eventually the writing is executed with the \c run() |
| 375 | 375 |
/// member function. A map writing rule can be added to the writer |
| 376 | 376 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
| 377 | 377 |
/// converter parameter can also be added as a standard functor |
| 378 | 378 |
/// converting from the value type of the map to \c std::string. If it |
| 379 | 379 |
/// is set, it will determine how the value type of the map is written to |
| 380 | 380 |
/// the output stream. If the functor is not set, then a default |
| 381 | 381 |
/// conversion will be used. The \c attribute(), \c node() and \c |
| 382 | 382 |
/// arc() functions are used to add attribute writing rules. |
| 383 | 383 |
/// |
| 384 | 384 |
///\code |
| 385 |
/// DigraphWriter<Digraph>(std::cout |
|
| 385 |
/// DigraphWriter<Digraph>(digraph, std::cout). |
|
| 386 | 386 |
/// nodeMap("coordinates", coord_map).
|
| 387 | 387 |
/// nodeMap("size", size).
|
| 388 | 388 |
/// nodeMap("title", title).
|
| 389 | 389 |
/// arcMap("capacity", cap_map).
|
| 390 | 390 |
/// node("source", src).
|
| 391 | 391 |
/// node("target", trg).
|
| 392 | 392 |
/// attribute("caption", caption).
|
| 393 | 393 |
/// run(); |
| 394 | 394 |
///\endcode |
| 395 | 395 |
/// |
| 396 | 396 |
/// |
| 397 | 397 |
/// By default, the writer does not write additional captions to the |
| 398 | 398 |
/// sections, but they can be give as an optional parameter of |
| 399 | 399 |
/// the \c nodes(), \c arcs() or \c |
| 400 | 400 |
/// attributes() functions. |
| 401 | 401 |
/// |
| 402 | 402 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
| 403 | 403 |
/// writing of the sections. If two arc sections should be written |
| 404 | 404 |
/// to the output, it can be done in two passes, the first pass |
| 405 | 405 |
/// writes the node section and the first arc section, then the |
| 406 | 406 |
/// second pass skips the node section and writes just the arc |
| 407 | 407 |
/// section to the stream. The output stream can be retrieved with |
| 408 | 408 |
/// the \c ostream() function, hence the second pass can append its |
| 409 | 409 |
/// output to the output of the first pass. |
| 410 | 410 |
template <typename _Digraph> |
| 411 | 411 |
class DigraphWriter {
|
| 412 | 412 |
public: |
| 413 | 413 |
|
| 414 | 414 |
typedef _Digraph Digraph; |
| 415 | 415 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 416 | 416 |
|
| 417 | 417 |
private: |
| 418 | 418 |
|
| 419 | 419 |
|
| 420 | 420 |
std::ostream* _os; |
| 421 | 421 |
bool local_os; |
| 422 | 422 |
|
| 423 | 423 |
const Digraph& _digraph; |
| 424 | 424 |
|
| 425 | 425 |
std::string _nodes_caption; |
| 426 | 426 |
std::string _arcs_caption; |
| 427 | 427 |
std::string _attributes_caption; |
| 428 | 428 |
|
| 429 | 429 |
typedef std::map<Node, std::string> NodeIndex; |
| 430 | 430 |
NodeIndex _node_index; |
| 431 | 431 |
typedef std::map<Arc, std::string> ArcIndex; |
| 432 | 432 |
ArcIndex _arc_index; |
| 433 | 433 |
|
| 434 | 434 |
typedef std::vector<std::pair<std::string, |
| 435 | 435 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 436 | 436 |
NodeMaps _node_maps; |
| 437 | 437 |
|
| 438 | 438 |
typedef std::vector<std::pair<std::string, |
| 439 | 439 |
_writer_bits::MapStorageBase<Arc>* > >ArcMaps; |
| 440 | 440 |
ArcMaps _arc_maps; |
| 441 | 441 |
|
| 442 | 442 |
typedef std::vector<std::pair<std::string, |
| 443 | 443 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 444 | 444 |
Attributes _attributes; |
| 445 | 445 |
|
| 446 | 446 |
bool _skip_nodes; |
| 447 | 447 |
bool _skip_arcs; |
| 448 | 448 |
|
| 449 | 449 |
public: |
| 450 | 450 |
|
| 451 | 451 |
/// \brief Constructor |
| 452 | 452 |
/// |
| 453 | 453 |
/// Construct a directed graph writer, which writes to the given |
| 454 | 454 |
/// output stream. |
| 455 |
DigraphWriter(std::ostream& is, const Digraph& digraph) |
|
| 456 |
: _os(&is), local_os(false), _digraph(digraph), |
|
| 455 |
DigraphWriter(const Digraph& digraph, std::ostream& os = std::cout) |
|
| 456 |
: _os(&os), local_os(false), _digraph(digraph), |
|
| 457 | 457 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 458 | 458 |
|
| 459 | 459 |
/// \brief Constructor |
| 460 | 460 |
/// |
| 461 | 461 |
/// Construct a directed graph writer, which writes to the given |
| 462 | 462 |
/// output file. |
| 463 |
DigraphWriter(const std::string& fn |
|
| 463 |
DigraphWriter(const Digraph& digraph, const std::string& fn) |
|
| 464 | 464 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), |
| 465 | 465 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 466 | 466 |
|
| 467 | 467 |
/// \brief Constructor |
| 468 | 468 |
/// |
| 469 | 469 |
/// Construct a directed graph writer, which writes to the given |
| 470 | 470 |
/// output file. |
| 471 |
DigraphWriter(const char* fn |
|
| 471 |
DigraphWriter(const Digraph& digraph, const char* fn) |
|
| 472 | 472 |
: _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), |
| 473 | 473 |
_skip_nodes(false), _skip_arcs(false) {}
|
| 474 | 474 |
|
| 475 | 475 |
/// \brief Destructor |
| 476 | 476 |
~DigraphWriter() {
|
| 477 | 477 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 478 | 478 |
it != _node_maps.end(); ++it) {
|
| 479 | 479 |
delete it->second; |
| 480 | 480 |
} |
| 481 | 481 |
|
| 482 | 482 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 483 | 483 |
it != _arc_maps.end(); ++it) {
|
| 484 | 484 |
delete it->second; |
| 485 | 485 |
} |
| 486 | 486 |
|
| 487 | 487 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 488 | 488 |
it != _attributes.end(); ++it) {
|
| 489 | 489 |
delete it->second; |
| 490 | 490 |
} |
| 491 | 491 |
|
| 492 | 492 |
if (local_os) {
|
| 493 | 493 |
delete _os; |
| 494 | 494 |
} |
| 495 | 495 |
} |
| 496 | 496 |
|
| 497 | 497 |
private: |
| 498 | 498 |
|
| 499 |
friend DigraphWriter<Digraph> digraphWriter<>(std::ostream& os, |
|
| 500 |
const Digraph& digraph); |
|
| 501 |
friend DigraphWriter<Digraph> digraphWriter<>(const std::string& fn, |
|
| 502 |
const Digraph& digraph); |
|
| 503 |
friend DigraphWriter<Digraph> digraphWriter<>(const char *fn, |
|
| 504 |
const Digraph& digraph); |
|
| 499 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
|
| 500 |
std::ostream& os); |
|
| 501 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
|
| 502 |
const std::string& fn); |
|
| 503 |
friend DigraphWriter<Digraph> digraphWriter<>(const Digraph& digraph, |
|
| 504 |
const char *fn); |
|
| 505 | 505 |
|
| 506 | 506 |
DigraphWriter(DigraphWriter& other) |
| 507 | 507 |
: _os(other._os), local_os(other.local_os), _digraph(other._digraph), |
| 508 | 508 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
| 509 | 509 |
|
| 510 | 510 |
other._os = 0; |
| 511 | 511 |
other.local_os = false; |
| 512 | 512 |
|
| 513 | 513 |
_node_index.swap(other._node_index); |
| 514 | 514 |
_arc_index.swap(other._arc_index); |
| 515 | 515 |
|
| 516 | 516 |
_node_maps.swap(other._node_maps); |
| 517 | 517 |
_arc_maps.swap(other._arc_maps); |
| 518 | 518 |
_attributes.swap(other._attributes); |
| 519 | 519 |
|
| 520 | 520 |
_nodes_caption = other._nodes_caption; |
| 521 | 521 |
_arcs_caption = other._arcs_caption; |
| 522 | 522 |
_attributes_caption = other._attributes_caption; |
| 523 | 523 |
} |
| 524 | 524 |
|
| 525 | 525 |
DigraphWriter& operator=(const DigraphWriter&); |
| 526 | 526 |
|
| 527 | 527 |
public: |
| 528 | 528 |
|
| 529 | 529 |
/// \name Writing rules |
| 530 | 530 |
/// @{
|
| 531 | 531 |
|
| 532 | 532 |
/// \brief Node map writing rule |
| 533 | 533 |
/// |
| 534 | 534 |
/// Add a node map writing rule to the writer. |
| 535 | 535 |
template <typename Map> |
| 536 | 536 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 537 | 537 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 538 | 538 |
_writer_bits::MapStorageBase<Node>* storage = |
| 539 | 539 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 540 | 540 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 541 | 541 |
return *this; |
| 542 | 542 |
} |
| 543 | 543 |
|
| 544 | 544 |
/// \brief Node map writing rule |
| 545 | 545 |
/// |
| 546 | 546 |
/// Add a node map writing rule with specialized converter to the |
| 547 | 547 |
/// writer. |
| 548 | 548 |
template <typename Map, typename Converter> |
| 549 | 549 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 550 | 550 |
const Converter& converter = Converter()) {
|
| 551 | 551 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 552 | 552 |
_writer_bits::MapStorageBase<Node>* storage = |
| 553 | 553 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 554 | 554 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 555 | 555 |
return *this; |
| 556 | 556 |
} |
| 557 | 557 |
|
| 558 | 558 |
/// \brief Arc map writing rule |
| 559 | 559 |
/// |
| 560 | 560 |
/// Add an arc map writing rule to the writer. |
| 561 | 561 |
template <typename Map> |
| 562 | 562 |
DigraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 563 | 563 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 564 | 564 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 565 | 565 |
new _writer_bits::MapStorage<Arc, Map>(map); |
| 566 | 566 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 567 | 567 |
return *this; |
| 568 | 568 |
} |
| 569 | 569 |
|
| 570 | 570 |
/// \brief Arc map writing rule |
| 571 | 571 |
/// |
| 572 | 572 |
/// Add an arc map writing rule with specialized converter to the |
| 573 | 573 |
/// writer. |
| 574 | 574 |
template <typename Map, typename Converter> |
| 575 | 575 |
DigraphWriter& arcMap(const std::string& caption, const Map& map, |
| 576 | 576 |
const Converter& converter = Converter()) {
|
| 577 | 577 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 578 | 578 |
_writer_bits::MapStorageBase<Arc>* storage = |
| 579 | 579 |
new _writer_bits::MapStorage<Arc, Map, Converter>(map, converter); |
| 580 | 580 |
_arc_maps.push_back(std::make_pair(caption, storage)); |
| 581 | 581 |
return *this; |
| 582 | 582 |
} |
| 583 | 583 |
|
| 584 | 584 |
/// \brief Attribute writing rule |
| 585 | 585 |
/// |
| 586 | 586 |
/// Add an attribute writing rule to the writer. |
| 587 | 587 |
template <typename Value> |
| 588 | 588 |
DigraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 589 | 589 |
_writer_bits::ValueStorageBase* storage = |
| 590 | 590 |
new _writer_bits::ValueStorage<Value>(value); |
| 591 | 591 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 592 | 592 |
return *this; |
| 593 | 593 |
} |
| 594 | 594 |
|
| 595 | 595 |
/// \brief Attribute writing rule |
| 596 | 596 |
/// |
| 597 | 597 |
/// Add an attribute writing rule with specialized converter to the |
| 598 | 598 |
/// writer. |
| 599 | 599 |
template <typename Value, typename Converter> |
| 600 | 600 |
DigraphWriter& attribute(const std::string& caption, const Value& value, |
| 601 | 601 |
const Converter& converter = Converter()) {
|
| 602 | 602 |
_writer_bits::ValueStorageBase* storage = |
| 603 | 603 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 604 | 604 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 605 | 605 |
return *this; |
| 606 | 606 |
} |
| 607 | 607 |
|
| 608 | 608 |
/// \brief Node writing rule |
| 609 | 609 |
/// |
| 610 | 610 |
/// Add a node writing rule to the writer. |
| 611 | 611 |
DigraphWriter& node(const std::string& caption, const Node& node) {
|
| 612 | 612 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 613 | 613 |
Converter converter(_node_index); |
| 614 | 614 |
_writer_bits::ValueStorageBase* storage = |
| 615 | 615 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 616 | 616 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 617 | 617 |
return *this; |
| 618 | 618 |
} |
| 619 | 619 |
|
| 620 | 620 |
/// \brief Arc writing rule |
| 621 | 621 |
/// |
| 622 | 622 |
/// Add an arc writing rule to writer. |
| 623 | 623 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 624 | 624 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
| 625 | 625 |
Converter converter(_arc_index); |
| 626 | 626 |
_writer_bits::ValueStorageBase* storage = |
| 627 | 627 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 628 | 628 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 629 | 629 |
return *this; |
| 630 | 630 |
} |
| 631 | 631 |
|
| 632 | 632 |
/// \name Section captions |
| 633 | 633 |
/// @{
|
| 634 | 634 |
|
| 635 | 635 |
/// \brief Add an additional caption to the \c \@nodes section |
| 636 | 636 |
/// |
| 637 | 637 |
/// Add an additional caption to the \c \@nodes section. |
| 638 | 638 |
DigraphWriter& nodes(const std::string& caption) {
|
| 639 | 639 |
_nodes_caption = caption; |
| 640 | 640 |
return *this; |
| 641 | 641 |
} |
| 642 | 642 |
|
| 643 | 643 |
/// \brief Add an additional caption to the \c \@arcs section |
| 644 | 644 |
/// |
| 645 | 645 |
/// Add an additional caption to the \c \@arcs section. |
| 646 | 646 |
DigraphWriter& arcs(const std::string& caption) {
|
| 647 | 647 |
_arcs_caption = caption; |
| 648 | 648 |
return *this; |
| 649 | 649 |
} |
| 650 | 650 |
|
| 651 | 651 |
/// \brief Add an additional caption to the \c \@attributes section |
| 652 | 652 |
/// |
| 653 | 653 |
/// Add an additional caption to the \c \@attributes section. |
| 654 | 654 |
DigraphWriter& attributes(const std::string& caption) {
|
| 655 | 655 |
_attributes_caption = caption; |
| 656 | 656 |
return *this; |
| 657 | 657 |
} |
| 658 | 658 |
|
| 659 | 659 |
/// \name Skipping section |
| 660 | 660 |
/// @{
|
| 661 | 661 |
|
| 662 | 662 |
/// \brief Skip writing the node set |
| 663 | 663 |
/// |
| 664 | 664 |
/// The \c \@nodes section will not be written to the stream. |
| 665 | 665 |
DigraphWriter& skipNodes() {
|
| 666 | 666 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
| 667 | 667 |
_skip_nodes = true; |
| 668 | 668 |
return *this; |
| 669 | 669 |
} |
| 670 | 670 |
|
| 671 | 671 |
/// \brief Skip writing arc set |
| 672 | 672 |
/// |
| 673 | 673 |
/// The \c \@arcs section will not be written to the stream. |
| 674 | 674 |
DigraphWriter& skipArcs() {
|
| 675 | 675 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
| 676 | 676 |
_skip_arcs = true; |
| 677 | 677 |
return *this; |
| 678 | 678 |
} |
| 679 | 679 |
|
| 680 | 680 |
/// @} |
| 681 | 681 |
|
| 682 | 682 |
private: |
| 683 | 683 |
|
| 684 | 684 |
void writeNodes() {
|
| 685 | 685 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 686 | 686 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 687 | 687 |
it != _node_maps.end(); ++it) {
|
| 688 | 688 |
if (it->first == "label") {
|
| 689 | 689 |
label = it->second; |
| 690 | 690 |
break; |
| 691 | 691 |
} |
| 692 | 692 |
} |
| 693 | 693 |
|
| 694 | 694 |
*_os << "@nodes"; |
| 695 | 695 |
if (!_nodes_caption.empty()) {
|
| 696 | 696 |
_writer_bits::writeToken(*_os << ' ', _nodes_caption); |
| ... | ... |
@@ -719,534 +719,535 @@ |
| 719 | 719 |
label->sort(nodes); |
| 720 | 720 |
} |
| 721 | 721 |
|
| 722 | 722 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 723 | 723 |
Node n = nodes[i]; |
| 724 | 724 |
if (label == 0) {
|
| 725 | 725 |
std::ostringstream os; |
| 726 | 726 |
os << _digraph.id(n); |
| 727 | 727 |
_writer_bits::writeToken(*_os, os.str()); |
| 728 | 728 |
*_os << '\t'; |
| 729 | 729 |
_node_index.insert(std::make_pair(n, os.str())); |
| 730 | 730 |
} |
| 731 | 731 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 732 | 732 |
it != _node_maps.end(); ++it) {
|
| 733 | 733 |
std::string value = it->second->get(n); |
| 734 | 734 |
_writer_bits::writeToken(*_os, value); |
| 735 | 735 |
if (it->first == "label") {
|
| 736 | 736 |
_node_index.insert(std::make_pair(n, value)); |
| 737 | 737 |
} |
| 738 | 738 |
*_os << '\t'; |
| 739 | 739 |
} |
| 740 | 740 |
*_os << std::endl; |
| 741 | 741 |
} |
| 742 | 742 |
} |
| 743 | 743 |
|
| 744 | 744 |
void createNodeIndex() {
|
| 745 | 745 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 746 | 746 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 747 | 747 |
it != _node_maps.end(); ++it) {
|
| 748 | 748 |
if (it->first == "label") {
|
| 749 | 749 |
label = it->second; |
| 750 | 750 |
break; |
| 751 | 751 |
} |
| 752 | 752 |
} |
| 753 | 753 |
|
| 754 | 754 |
if (label == 0) {
|
| 755 | 755 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 756 | 756 |
std::ostringstream os; |
| 757 | 757 |
os << _digraph.id(n); |
| 758 | 758 |
_node_index.insert(std::make_pair(n, os.str())); |
| 759 | 759 |
} |
| 760 | 760 |
} else {
|
| 761 | 761 |
for (NodeIt n(_digraph); n != INVALID; ++n) {
|
| 762 | 762 |
std::string value = label->get(n); |
| 763 | 763 |
_node_index.insert(std::make_pair(n, value)); |
| 764 | 764 |
} |
| 765 | 765 |
} |
| 766 | 766 |
} |
| 767 | 767 |
|
| 768 | 768 |
void writeArcs() {
|
| 769 | 769 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 770 | 770 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 771 | 771 |
it != _arc_maps.end(); ++it) {
|
| 772 | 772 |
if (it->first == "label") {
|
| 773 | 773 |
label = it->second; |
| 774 | 774 |
break; |
| 775 | 775 |
} |
| 776 | 776 |
} |
| 777 | 777 |
|
| 778 | 778 |
*_os << "@arcs"; |
| 779 | 779 |
if (!_arcs_caption.empty()) {
|
| 780 | 780 |
_writer_bits::writeToken(*_os << ' ', _arcs_caption); |
| 781 | 781 |
} |
| 782 | 782 |
*_os << std::endl; |
| 783 | 783 |
|
| 784 | 784 |
*_os << '\t' << '\t'; |
| 785 | 785 |
if (label == 0) {
|
| 786 | 786 |
*_os << "label" << '\t'; |
| 787 | 787 |
} |
| 788 | 788 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 789 | 789 |
it != _arc_maps.end(); ++it) {
|
| 790 | 790 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 791 | 791 |
} |
| 792 | 792 |
*_os << std::endl; |
| 793 | 793 |
|
| 794 | 794 |
std::vector<Arc> arcs; |
| 795 | 795 |
for (ArcIt n(_digraph); n != INVALID; ++n) {
|
| 796 | 796 |
arcs.push_back(n); |
| 797 | 797 |
} |
| 798 | 798 |
|
| 799 | 799 |
if (label == 0) {
|
| 800 | 800 |
IdMap<Digraph, Arc> id_map(_digraph); |
| 801 | 801 |
_writer_bits::MapLess<IdMap<Digraph, Arc> > id_less(id_map); |
| 802 | 802 |
std::sort(arcs.begin(), arcs.end(), id_less); |
| 803 | 803 |
} else {
|
| 804 | 804 |
label->sort(arcs); |
| 805 | 805 |
} |
| 806 | 806 |
|
| 807 | 807 |
for (int i = 0; i < static_cast<int>(arcs.size()); ++i) {
|
| 808 | 808 |
Arc a = arcs[i]; |
| 809 | 809 |
_writer_bits::writeToken(*_os, _node_index. |
| 810 | 810 |
find(_digraph.source(a))->second); |
| 811 | 811 |
*_os << '\t'; |
| 812 | 812 |
_writer_bits::writeToken(*_os, _node_index. |
| 813 | 813 |
find(_digraph.target(a))->second); |
| 814 | 814 |
*_os << '\t'; |
| 815 | 815 |
if (label == 0) {
|
| 816 | 816 |
std::ostringstream os; |
| 817 | 817 |
os << _digraph.id(a); |
| 818 | 818 |
_writer_bits::writeToken(*_os, os.str()); |
| 819 | 819 |
*_os << '\t'; |
| 820 | 820 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 821 | 821 |
} |
| 822 | 822 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 823 | 823 |
it != _arc_maps.end(); ++it) {
|
| 824 | 824 |
std::string value = it->second->get(a); |
| 825 | 825 |
_writer_bits::writeToken(*_os, value); |
| 826 | 826 |
if (it->first == "label") {
|
| 827 | 827 |
_arc_index.insert(std::make_pair(a, value)); |
| 828 | 828 |
} |
| 829 | 829 |
*_os << '\t'; |
| 830 | 830 |
} |
| 831 | 831 |
*_os << std::endl; |
| 832 | 832 |
} |
| 833 | 833 |
} |
| 834 | 834 |
|
| 835 | 835 |
void createArcIndex() {
|
| 836 | 836 |
_writer_bits::MapStorageBase<Arc>* label = 0; |
| 837 | 837 |
for (typename ArcMaps::iterator it = _arc_maps.begin(); |
| 838 | 838 |
it != _arc_maps.end(); ++it) {
|
| 839 | 839 |
if (it->first == "label") {
|
| 840 | 840 |
label = it->second; |
| 841 | 841 |
break; |
| 842 | 842 |
} |
| 843 | 843 |
} |
| 844 | 844 |
|
| 845 | 845 |
if (label == 0) {
|
| 846 | 846 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 847 | 847 |
std::ostringstream os; |
| 848 | 848 |
os << _digraph.id(a); |
| 849 | 849 |
_arc_index.insert(std::make_pair(a, os.str())); |
| 850 | 850 |
} |
| 851 | 851 |
} else {
|
| 852 | 852 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
| 853 | 853 |
std::string value = label->get(a); |
| 854 | 854 |
_arc_index.insert(std::make_pair(a, value)); |
| 855 | 855 |
} |
| 856 | 856 |
} |
| 857 | 857 |
} |
| 858 | 858 |
|
| 859 | 859 |
void writeAttributes() {
|
| 860 | 860 |
if (_attributes.empty()) return; |
| 861 | 861 |
*_os << "@attributes"; |
| 862 | 862 |
if (!_attributes_caption.empty()) {
|
| 863 | 863 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 864 | 864 |
} |
| 865 | 865 |
*_os << std::endl; |
| 866 | 866 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 867 | 867 |
it != _attributes.end(); ++it) {
|
| 868 | 868 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 869 | 869 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 870 | 870 |
*_os << std::endl; |
| 871 | 871 |
} |
| 872 | 872 |
} |
| 873 | 873 |
|
| 874 | 874 |
public: |
| 875 | 875 |
|
| 876 | 876 |
/// \name Execution of the writer |
| 877 | 877 |
/// @{
|
| 878 | 878 |
|
| 879 | 879 |
/// \brief Start the batch processing |
| 880 | 880 |
/// |
| 881 | 881 |
/// This function starts the batch processing. |
| 882 | 882 |
void run() {
|
| 883 | 883 |
if (!_skip_nodes) {
|
| 884 | 884 |
writeNodes(); |
| 885 | 885 |
} else {
|
| 886 | 886 |
createNodeIndex(); |
| 887 | 887 |
} |
| 888 | 888 |
if (!_skip_arcs) {
|
| 889 | 889 |
writeArcs(); |
| 890 | 890 |
} else {
|
| 891 | 891 |
createArcIndex(); |
| 892 | 892 |
} |
| 893 | 893 |
writeAttributes(); |
| 894 | 894 |
} |
| 895 | 895 |
|
| 896 | 896 |
/// \brief Give back the stream of the writer |
| 897 | 897 |
/// |
| 898 | 898 |
/// Give back the stream of the writer. |
| 899 | 899 |
std::ostream& ostream() {
|
| 900 | 900 |
return *_os; |
| 901 | 901 |
} |
| 902 | 902 |
|
| 903 | 903 |
/// @} |
| 904 | 904 |
}; |
| 905 | 905 |
|
| 906 | 906 |
/// \brief Return a \ref DigraphWriter class |
| 907 | 907 |
/// |
| 908 | 908 |
/// This function just returns a \ref DigraphWriter class. |
| 909 | 909 |
/// \relates DigraphWriter |
| 910 | 910 |
template <typename Digraph> |
| 911 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os, |
|
| 912 |
const Digraph& digraph) {
|
|
| 913 |
|
|
| 911 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 912 |
std::ostream& os = std::cout) {
|
|
| 913 |
DigraphWriter<Digraph> tmp(digraph, os); |
|
| 914 | 914 |
return tmp; |
| 915 | 915 |
} |
| 916 | 916 |
|
| 917 | 917 |
/// \brief Return a \ref DigraphWriter class |
| 918 | 918 |
/// |
| 919 | 919 |
/// This function just returns a \ref DigraphWriter class. |
| 920 | 920 |
/// \relates DigraphWriter |
| 921 | 921 |
template <typename Digraph> |
| 922 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
|
| 923 |
const Digraph& digraph) {
|
|
| 924 |
|
|
| 922 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 923 |
const std::string& fn) {
|
|
| 924 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 925 | 925 |
return tmp; |
| 926 | 926 |
} |
| 927 | 927 |
|
| 928 | 928 |
/// \brief Return a \ref DigraphWriter class |
| 929 | 929 |
/// |
| 930 | 930 |
/// This function just returns a \ref DigraphWriter class. |
| 931 | 931 |
/// \relates DigraphWriter |
| 932 | 932 |
template <typename Digraph> |
| 933 |
DigraphWriter<Digraph> digraphWriter(const char* fn, |
|
| 934 |
const Digraph& digraph) {
|
|
| 935 |
|
|
| 933 |
DigraphWriter<Digraph> digraphWriter(const Digraph& digraph, |
|
| 934 |
const char* fn) {
|
|
| 935 |
DigraphWriter<Digraph> tmp(digraph, fn); |
|
| 936 | 936 |
return tmp; |
| 937 | 937 |
} |
| 938 | 938 |
|
| 939 | 939 |
template <typename Graph> |
| 940 | 940 |
class GraphWriter; |
| 941 | 941 |
|
| 942 | 942 |
template <typename Graph> |
| 943 |
GraphWriter<Graph> graphWriter( |
|
| 943 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
|
| 944 |
std::ostream& os = std::cout); |
|
| 944 | 945 |
|
| 945 | 946 |
template <typename Graph> |
| 946 |
GraphWriter<Graph> graphWriter(const std::string& fn |
|
| 947 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn); |
|
| 947 | 948 |
|
| 948 | 949 |
template <typename Graph> |
| 949 |
GraphWriter<Graph> graphWriter(const |
|
| 950 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char *fn); |
|
| 950 | 951 |
|
| 951 | 952 |
/// \ingroup lemon_io |
| 952 | 953 |
/// |
| 953 | 954 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
| 954 | 955 |
/// |
| 955 | 956 |
/// This utility writes an \ref lgf-format "LGF" file. |
| 956 | 957 |
/// |
| 957 | 958 |
/// It can be used almost the same way as \c DigraphWriter. |
| 958 | 959 |
/// The only difference is that this class can handle edges and |
| 959 | 960 |
/// edge maps as well as arcs and arc maps. |
| 960 | 961 |
/// |
| 961 | 962 |
/// The arc maps are written into the file as two columns, the |
| 962 | 963 |
/// caption of the columns are the name of the map prefixed with \c |
| 963 | 964 |
/// '+' and \c '-'. The arcs are written into the \c \@attributes |
| 964 | 965 |
/// section as a \c '+' or a \c '-' prefix (depends on the direction |
| 965 | 966 |
/// of the arc) and the label of corresponding edge. |
| 966 | 967 |
template <typename _Graph> |
| 967 | 968 |
class GraphWriter {
|
| 968 | 969 |
public: |
| 969 | 970 |
|
| 970 | 971 |
typedef _Graph Graph; |
| 971 | 972 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
| 972 | 973 |
|
| 973 | 974 |
private: |
| 974 | 975 |
|
| 975 | 976 |
|
| 976 | 977 |
std::ostream* _os; |
| 977 | 978 |
bool local_os; |
| 978 | 979 |
|
| 979 | 980 |
const Graph& _graph; |
| 980 | 981 |
|
| 981 | 982 |
std::string _nodes_caption; |
| 982 | 983 |
std::string _edges_caption; |
| 983 | 984 |
std::string _attributes_caption; |
| 984 | 985 |
|
| 985 | 986 |
typedef std::map<Node, std::string> NodeIndex; |
| 986 | 987 |
NodeIndex _node_index; |
| 987 | 988 |
typedef std::map<Edge, std::string> EdgeIndex; |
| 988 | 989 |
EdgeIndex _edge_index; |
| 989 | 990 |
|
| 990 | 991 |
typedef std::vector<std::pair<std::string, |
| 991 | 992 |
_writer_bits::MapStorageBase<Node>* > > NodeMaps; |
| 992 | 993 |
NodeMaps _node_maps; |
| 993 | 994 |
|
| 994 | 995 |
typedef std::vector<std::pair<std::string, |
| 995 | 996 |
_writer_bits::MapStorageBase<Edge>* > >EdgeMaps; |
| 996 | 997 |
EdgeMaps _edge_maps; |
| 997 | 998 |
|
| 998 | 999 |
typedef std::vector<std::pair<std::string, |
| 999 | 1000 |
_writer_bits::ValueStorageBase*> > Attributes; |
| 1000 | 1001 |
Attributes _attributes; |
| 1001 | 1002 |
|
| 1002 | 1003 |
bool _skip_nodes; |
| 1003 | 1004 |
bool _skip_edges; |
| 1004 | 1005 |
|
| 1005 | 1006 |
public: |
| 1006 | 1007 |
|
| 1007 | 1008 |
/// \brief Constructor |
| 1008 | 1009 |
/// |
| 1009 | 1010 |
/// Construct a directed graph writer, which writes to the given |
| 1010 | 1011 |
/// output stream. |
| 1011 |
GraphWriter(std::ostream& is, const Graph& graph) |
|
| 1012 |
: _os(&is), local_os(false), _graph(graph), |
|
| 1012 |
GraphWriter(const Graph& graph, std::ostream& os = std::cout) |
|
| 1013 |
: _os(&os), local_os(false), _graph(graph), |
|
| 1013 | 1014 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1014 | 1015 |
|
| 1015 | 1016 |
/// \brief Constructor |
| 1016 | 1017 |
/// |
| 1017 | 1018 |
/// Construct a directed graph writer, which writes to the given |
| 1018 | 1019 |
/// output file. |
| 1019 |
GraphWriter(const std::string& fn |
|
| 1020 |
GraphWriter(const Graph& graph, const std::string& fn) |
|
| 1020 | 1021 |
: _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), |
| 1021 | 1022 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1022 | 1023 |
|
| 1023 | 1024 |
/// \brief Constructor |
| 1024 | 1025 |
/// |
| 1025 | 1026 |
/// Construct a directed graph writer, which writes to the given |
| 1026 | 1027 |
/// output file. |
| 1027 |
GraphWriter(const char* fn |
|
| 1028 |
GraphWriter(const Graph& graph, const char* fn) |
|
| 1028 | 1029 |
: _os(new std::ofstream(fn)), local_os(true), _graph(graph), |
| 1029 | 1030 |
_skip_nodes(false), _skip_edges(false) {}
|
| 1030 | 1031 |
|
| 1031 | 1032 |
/// \brief Destructor |
| 1032 | 1033 |
~GraphWriter() {
|
| 1033 | 1034 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1034 | 1035 |
it != _node_maps.end(); ++it) {
|
| 1035 | 1036 |
delete it->second; |
| 1036 | 1037 |
} |
| 1037 | 1038 |
|
| 1038 | 1039 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1039 | 1040 |
it != _edge_maps.end(); ++it) {
|
| 1040 | 1041 |
delete it->second; |
| 1041 | 1042 |
} |
| 1042 | 1043 |
|
| 1043 | 1044 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1044 | 1045 |
it != _attributes.end(); ++it) {
|
| 1045 | 1046 |
delete it->second; |
| 1046 | 1047 |
} |
| 1047 | 1048 |
|
| 1048 | 1049 |
if (local_os) {
|
| 1049 | 1050 |
delete _os; |
| 1050 | 1051 |
} |
| 1051 | 1052 |
} |
| 1052 | 1053 |
|
| 1053 | 1054 |
private: |
| 1054 | 1055 |
|
| 1055 |
friend GraphWriter<Graph> graphWriter<>(std::ostream& os, |
|
| 1056 |
const Graph& graph); |
|
| 1057 |
friend GraphWriter<Graph> graphWriter<>(const std::string& fn, |
|
| 1058 |
const Graph& graph); |
|
| 1059 |
friend GraphWriter<Graph> graphWriter<>(const char *fn, |
|
| 1060 |
const Graph& graph); |
|
| 1056 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
|
| 1057 |
std::ostream& os); |
|
| 1058 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
|
| 1059 |
const std::string& fn); |
|
| 1060 |
friend GraphWriter<Graph> graphWriter<>(const Graph& graph, |
|
| 1061 |
const char *fn); |
|
| 1061 | 1062 |
|
| 1062 | 1063 |
GraphWriter(GraphWriter& other) |
| 1063 | 1064 |
: _os(other._os), local_os(other.local_os), _graph(other._graph), |
| 1064 | 1065 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
| 1065 | 1066 |
|
| 1066 | 1067 |
other._os = 0; |
| 1067 | 1068 |
other.local_os = false; |
| 1068 | 1069 |
|
| 1069 | 1070 |
_node_index.swap(other._node_index); |
| 1070 | 1071 |
_edge_index.swap(other._edge_index); |
| 1071 | 1072 |
|
| 1072 | 1073 |
_node_maps.swap(other._node_maps); |
| 1073 | 1074 |
_edge_maps.swap(other._edge_maps); |
| 1074 | 1075 |
_attributes.swap(other._attributes); |
| 1075 | 1076 |
|
| 1076 | 1077 |
_nodes_caption = other._nodes_caption; |
| 1077 | 1078 |
_edges_caption = other._edges_caption; |
| 1078 | 1079 |
_attributes_caption = other._attributes_caption; |
| 1079 | 1080 |
} |
| 1080 | 1081 |
|
| 1081 | 1082 |
GraphWriter& operator=(const GraphWriter&); |
| 1082 | 1083 |
|
| 1083 | 1084 |
public: |
| 1084 | 1085 |
|
| 1085 | 1086 |
/// \name Writing rules |
| 1086 | 1087 |
/// @{
|
| 1087 | 1088 |
|
| 1088 | 1089 |
/// \brief Node map writing rule |
| 1089 | 1090 |
/// |
| 1090 | 1091 |
/// Add a node map writing rule to the writer. |
| 1091 | 1092 |
template <typename Map> |
| 1092 | 1093 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) {
|
| 1093 | 1094 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1094 | 1095 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1095 | 1096 |
new _writer_bits::MapStorage<Node, Map>(map); |
| 1096 | 1097 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1097 | 1098 |
return *this; |
| 1098 | 1099 |
} |
| 1099 | 1100 |
|
| 1100 | 1101 |
/// \brief Node map writing rule |
| 1101 | 1102 |
/// |
| 1102 | 1103 |
/// Add a node map writing rule with specialized converter to the |
| 1103 | 1104 |
/// writer. |
| 1104 | 1105 |
template <typename Map, typename Converter> |
| 1105 | 1106 |
GraphWriter& nodeMap(const std::string& caption, const Map& map, |
| 1106 | 1107 |
const Converter& converter = Converter()) {
|
| 1107 | 1108 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
| 1108 | 1109 |
_writer_bits::MapStorageBase<Node>* storage = |
| 1109 | 1110 |
new _writer_bits::MapStorage<Node, Map, Converter>(map, converter); |
| 1110 | 1111 |
_node_maps.push_back(std::make_pair(caption, storage)); |
| 1111 | 1112 |
return *this; |
| 1112 | 1113 |
} |
| 1113 | 1114 |
|
| 1114 | 1115 |
/// \brief Edge map writing rule |
| 1115 | 1116 |
/// |
| 1116 | 1117 |
/// Add an edge map writing rule to the writer. |
| 1117 | 1118 |
template <typename Map> |
| 1118 | 1119 |
GraphWriter& edgeMap(const std::string& caption, const Map& map) {
|
| 1119 | 1120 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1120 | 1121 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1121 | 1122 |
new _writer_bits::MapStorage<Edge, Map>(map); |
| 1122 | 1123 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1123 | 1124 |
return *this; |
| 1124 | 1125 |
} |
| 1125 | 1126 |
|
| 1126 | 1127 |
/// \brief Edge map writing rule |
| 1127 | 1128 |
/// |
| 1128 | 1129 |
/// Add an edge map writing rule with specialized converter to the |
| 1129 | 1130 |
/// writer. |
| 1130 | 1131 |
template <typename Map, typename Converter> |
| 1131 | 1132 |
GraphWriter& edgeMap(const std::string& caption, const Map& map, |
| 1132 | 1133 |
const Converter& converter = Converter()) {
|
| 1133 | 1134 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
| 1134 | 1135 |
_writer_bits::MapStorageBase<Edge>* storage = |
| 1135 | 1136 |
new _writer_bits::MapStorage<Edge, Map, Converter>(map, converter); |
| 1136 | 1137 |
_edge_maps.push_back(std::make_pair(caption, storage)); |
| 1137 | 1138 |
return *this; |
| 1138 | 1139 |
} |
| 1139 | 1140 |
|
| 1140 | 1141 |
/// \brief Arc map writing rule |
| 1141 | 1142 |
/// |
| 1142 | 1143 |
/// Add an arc map writing rule to the writer. |
| 1143 | 1144 |
template <typename Map> |
| 1144 | 1145 |
GraphWriter& arcMap(const std::string& caption, const Map& map) {
|
| 1145 | 1146 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1146 | 1147 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1147 | 1148 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map); |
| 1148 | 1149 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1149 | 1150 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1150 | 1151 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map); |
| 1151 | 1152 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1152 | 1153 |
return *this; |
| 1153 | 1154 |
} |
| 1154 | 1155 |
|
| 1155 | 1156 |
/// \brief Arc map writing rule |
| 1156 | 1157 |
/// |
| 1157 | 1158 |
/// Add an arc map writing rule with specialized converter to the |
| 1158 | 1159 |
/// writer. |
| 1159 | 1160 |
template <typename Map, typename Converter> |
| 1160 | 1161 |
GraphWriter& arcMap(const std::string& caption, const Map& map, |
| 1161 | 1162 |
const Converter& converter = Converter()) {
|
| 1162 | 1163 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
| 1163 | 1164 |
_writer_bits::MapStorageBase<Edge>* forward_storage = |
| 1164 | 1165 |
new _writer_bits::GraphArcMapStorage<Graph, true, Map, Converter> |
| 1165 | 1166 |
(_graph, map, converter); |
| 1166 | 1167 |
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
|
| 1167 | 1168 |
_writer_bits::MapStorageBase<Edge>* backward_storage = |
| 1168 | 1169 |
new _writer_bits::GraphArcMapStorage<Graph, false, Map, Converter> |
| 1169 | 1170 |
(_graph, map, converter); |
| 1170 | 1171 |
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
|
| 1171 | 1172 |
return *this; |
| 1172 | 1173 |
} |
| 1173 | 1174 |
|
| 1174 | 1175 |
/// \brief Attribute writing rule |
| 1175 | 1176 |
/// |
| 1176 | 1177 |
/// Add an attribute writing rule to the writer. |
| 1177 | 1178 |
template <typename Value> |
| 1178 | 1179 |
GraphWriter& attribute(const std::string& caption, const Value& value) {
|
| 1179 | 1180 |
_writer_bits::ValueStorageBase* storage = |
| 1180 | 1181 |
new _writer_bits::ValueStorage<Value>(value); |
| 1181 | 1182 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1182 | 1183 |
return *this; |
| 1183 | 1184 |
} |
| 1184 | 1185 |
|
| 1185 | 1186 |
/// \brief Attribute writing rule |
| 1186 | 1187 |
/// |
| 1187 | 1188 |
/// Add an attribute writing rule with specialized converter to the |
| 1188 | 1189 |
/// writer. |
| 1189 | 1190 |
template <typename Value, typename Converter> |
| 1190 | 1191 |
GraphWriter& attribute(const std::string& caption, const Value& value, |
| 1191 | 1192 |
const Converter& converter = Converter()) {
|
| 1192 | 1193 |
_writer_bits::ValueStorageBase* storage = |
| 1193 | 1194 |
new _writer_bits::ValueStorage<Value, Converter>(value, converter); |
| 1194 | 1195 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1195 | 1196 |
return *this; |
| 1196 | 1197 |
} |
| 1197 | 1198 |
|
| 1198 | 1199 |
/// \brief Node writing rule |
| 1199 | 1200 |
/// |
| 1200 | 1201 |
/// Add a node writing rule to the writer. |
| 1201 | 1202 |
GraphWriter& node(const std::string& caption, const Node& node) {
|
| 1202 | 1203 |
typedef _writer_bits::MapLookUpConverter<Node> Converter; |
| 1203 | 1204 |
Converter converter(_node_index); |
| 1204 | 1205 |
_writer_bits::ValueStorageBase* storage = |
| 1205 | 1206 |
new _writer_bits::ValueStorage<Node, Converter>(node, converter); |
| 1206 | 1207 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1207 | 1208 |
return *this; |
| 1208 | 1209 |
} |
| 1209 | 1210 |
|
| 1210 | 1211 |
/// \brief Edge writing rule |
| 1211 | 1212 |
/// |
| 1212 | 1213 |
/// Add an edge writing rule to writer. |
| 1213 | 1214 |
GraphWriter& edge(const std::string& caption, const Edge& edge) {
|
| 1214 | 1215 |
typedef _writer_bits::MapLookUpConverter<Edge> Converter; |
| 1215 | 1216 |
Converter converter(_edge_index); |
| 1216 | 1217 |
_writer_bits::ValueStorageBase* storage = |
| 1217 | 1218 |
new _writer_bits::ValueStorage<Edge, Converter>(edge, converter); |
| 1218 | 1219 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1219 | 1220 |
return *this; |
| 1220 | 1221 |
} |
| 1221 | 1222 |
|
| 1222 | 1223 |
/// \brief Arc writing rule |
| 1223 | 1224 |
/// |
| 1224 | 1225 |
/// Add an arc writing rule to writer. |
| 1225 | 1226 |
GraphWriter& arc(const std::string& caption, const Arc& arc) {
|
| 1226 | 1227 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter; |
| 1227 | 1228 |
Converter converter(_graph, _edge_index); |
| 1228 | 1229 |
_writer_bits::ValueStorageBase* storage = |
| 1229 | 1230 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
| 1230 | 1231 |
_attributes.push_back(std::make_pair(caption, storage)); |
| 1231 | 1232 |
return *this; |
| 1232 | 1233 |
} |
| 1233 | 1234 |
|
| 1234 | 1235 |
/// \name Section captions |
| 1235 | 1236 |
/// @{
|
| 1236 | 1237 |
|
| 1237 | 1238 |
/// \brief Add an additional caption to the \c \@nodes section |
| 1238 | 1239 |
/// |
| 1239 | 1240 |
/// Add an additional caption to the \c \@nodes section. |
| 1240 | 1241 |
GraphWriter& nodes(const std::string& caption) {
|
| 1241 | 1242 |
_nodes_caption = caption; |
| 1242 | 1243 |
return *this; |
| 1243 | 1244 |
} |
| 1244 | 1245 |
|
| 1245 | 1246 |
/// \brief Add an additional caption to the \c \@arcs section |
| 1246 | 1247 |
/// |
| 1247 | 1248 |
/// Add an additional caption to the \c \@arcs section. |
| 1248 | 1249 |
GraphWriter& edges(const std::string& caption) {
|
| 1249 | 1250 |
_edges_caption = caption; |
| 1250 | 1251 |
return *this; |
| 1251 | 1252 |
} |
| 1252 | 1253 |
|
| ... | ... |
@@ -1321,406 +1322,407 @@ |
| 1321 | 1322 |
label->sort(nodes); |
| 1322 | 1323 |
} |
| 1323 | 1324 |
|
| 1324 | 1325 |
for (int i = 0; i < static_cast<int>(nodes.size()); ++i) {
|
| 1325 | 1326 |
Node n = nodes[i]; |
| 1326 | 1327 |
if (label == 0) {
|
| 1327 | 1328 |
std::ostringstream os; |
| 1328 | 1329 |
os << _graph.id(n); |
| 1329 | 1330 |
_writer_bits::writeToken(*_os, os.str()); |
| 1330 | 1331 |
*_os << '\t'; |
| 1331 | 1332 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1332 | 1333 |
} |
| 1333 | 1334 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1334 | 1335 |
it != _node_maps.end(); ++it) {
|
| 1335 | 1336 |
std::string value = it->second->get(n); |
| 1336 | 1337 |
_writer_bits::writeToken(*_os, value); |
| 1337 | 1338 |
if (it->first == "label") {
|
| 1338 | 1339 |
_node_index.insert(std::make_pair(n, value)); |
| 1339 | 1340 |
} |
| 1340 | 1341 |
*_os << '\t'; |
| 1341 | 1342 |
} |
| 1342 | 1343 |
*_os << std::endl; |
| 1343 | 1344 |
} |
| 1344 | 1345 |
} |
| 1345 | 1346 |
|
| 1346 | 1347 |
void createNodeIndex() {
|
| 1347 | 1348 |
_writer_bits::MapStorageBase<Node>* label = 0; |
| 1348 | 1349 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
| 1349 | 1350 |
it != _node_maps.end(); ++it) {
|
| 1350 | 1351 |
if (it->first == "label") {
|
| 1351 | 1352 |
label = it->second; |
| 1352 | 1353 |
break; |
| 1353 | 1354 |
} |
| 1354 | 1355 |
} |
| 1355 | 1356 |
|
| 1356 | 1357 |
if (label == 0) {
|
| 1357 | 1358 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1358 | 1359 |
std::ostringstream os; |
| 1359 | 1360 |
os << _graph.id(n); |
| 1360 | 1361 |
_node_index.insert(std::make_pair(n, os.str())); |
| 1361 | 1362 |
} |
| 1362 | 1363 |
} else {
|
| 1363 | 1364 |
for (NodeIt n(_graph); n != INVALID; ++n) {
|
| 1364 | 1365 |
std::string value = label->get(n); |
| 1365 | 1366 |
_node_index.insert(std::make_pair(n, value)); |
| 1366 | 1367 |
} |
| 1367 | 1368 |
} |
| 1368 | 1369 |
} |
| 1369 | 1370 |
|
| 1370 | 1371 |
void writeEdges() {
|
| 1371 | 1372 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1372 | 1373 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1373 | 1374 |
it != _edge_maps.end(); ++it) {
|
| 1374 | 1375 |
if (it->first == "label") {
|
| 1375 | 1376 |
label = it->second; |
| 1376 | 1377 |
break; |
| 1377 | 1378 |
} |
| 1378 | 1379 |
} |
| 1379 | 1380 |
|
| 1380 | 1381 |
*_os << "@edges"; |
| 1381 | 1382 |
if (!_edges_caption.empty()) {
|
| 1382 | 1383 |
_writer_bits::writeToken(*_os << ' ', _edges_caption); |
| 1383 | 1384 |
} |
| 1384 | 1385 |
*_os << std::endl; |
| 1385 | 1386 |
|
| 1386 | 1387 |
*_os << '\t' << '\t'; |
| 1387 | 1388 |
if (label == 0) {
|
| 1388 | 1389 |
*_os << "label" << '\t'; |
| 1389 | 1390 |
} |
| 1390 | 1391 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1391 | 1392 |
it != _edge_maps.end(); ++it) {
|
| 1392 | 1393 |
_writer_bits::writeToken(*_os, it->first) << '\t'; |
| 1393 | 1394 |
} |
| 1394 | 1395 |
*_os << std::endl; |
| 1395 | 1396 |
|
| 1396 | 1397 |
std::vector<Edge> edges; |
| 1397 | 1398 |
for (EdgeIt n(_graph); n != INVALID; ++n) {
|
| 1398 | 1399 |
edges.push_back(n); |
| 1399 | 1400 |
} |
| 1400 | 1401 |
|
| 1401 | 1402 |
if (label == 0) {
|
| 1402 | 1403 |
IdMap<Graph, Edge> id_map(_graph); |
| 1403 | 1404 |
_writer_bits::MapLess<IdMap<Graph, Edge> > id_less(id_map); |
| 1404 | 1405 |
std::sort(edges.begin(), edges.end(), id_less); |
| 1405 | 1406 |
} else {
|
| 1406 | 1407 |
label->sort(edges); |
| 1407 | 1408 |
} |
| 1408 | 1409 |
|
| 1409 | 1410 |
for (int i = 0; i < static_cast<int>(edges.size()); ++i) {
|
| 1410 | 1411 |
Edge e = edges[i]; |
| 1411 | 1412 |
_writer_bits::writeToken(*_os, _node_index. |
| 1412 | 1413 |
find(_graph.u(e))->second); |
| 1413 | 1414 |
*_os << '\t'; |
| 1414 | 1415 |
_writer_bits::writeToken(*_os, _node_index. |
| 1415 | 1416 |
find(_graph.v(e))->second); |
| 1416 | 1417 |
*_os << '\t'; |
| 1417 | 1418 |
if (label == 0) {
|
| 1418 | 1419 |
std::ostringstream os; |
| 1419 | 1420 |
os << _graph.id(e); |
| 1420 | 1421 |
_writer_bits::writeToken(*_os, os.str()); |
| 1421 | 1422 |
*_os << '\t'; |
| 1422 | 1423 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1423 | 1424 |
} |
| 1424 | 1425 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1425 | 1426 |
it != _edge_maps.end(); ++it) {
|
| 1426 | 1427 |
std::string value = it->second->get(e); |
| 1427 | 1428 |
_writer_bits::writeToken(*_os, value); |
| 1428 | 1429 |
if (it->first == "label") {
|
| 1429 | 1430 |
_edge_index.insert(std::make_pair(e, value)); |
| 1430 | 1431 |
} |
| 1431 | 1432 |
*_os << '\t'; |
| 1432 | 1433 |
} |
| 1433 | 1434 |
*_os << std::endl; |
| 1434 | 1435 |
} |
| 1435 | 1436 |
} |
| 1436 | 1437 |
|
| 1437 | 1438 |
void createEdgeIndex() {
|
| 1438 | 1439 |
_writer_bits::MapStorageBase<Edge>* label = 0; |
| 1439 | 1440 |
for (typename EdgeMaps::iterator it = _edge_maps.begin(); |
| 1440 | 1441 |
it != _edge_maps.end(); ++it) {
|
| 1441 | 1442 |
if (it->first == "label") {
|
| 1442 | 1443 |
label = it->second; |
| 1443 | 1444 |
break; |
| 1444 | 1445 |
} |
| 1445 | 1446 |
} |
| 1446 | 1447 |
|
| 1447 | 1448 |
if (label == 0) {
|
| 1448 | 1449 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1449 | 1450 |
std::ostringstream os; |
| 1450 | 1451 |
os << _graph.id(e); |
| 1451 | 1452 |
_edge_index.insert(std::make_pair(e, os.str())); |
| 1452 | 1453 |
} |
| 1453 | 1454 |
} else {
|
| 1454 | 1455 |
for (EdgeIt e(_graph); e != INVALID; ++e) {
|
| 1455 | 1456 |
std::string value = label->get(e); |
| 1456 | 1457 |
_edge_index.insert(std::make_pair(e, value)); |
| 1457 | 1458 |
} |
| 1458 | 1459 |
} |
| 1459 | 1460 |
} |
| 1460 | 1461 |
|
| 1461 | 1462 |
void writeAttributes() {
|
| 1462 | 1463 |
if (_attributes.empty()) return; |
| 1463 | 1464 |
*_os << "@attributes"; |
| 1464 | 1465 |
if (!_attributes_caption.empty()) {
|
| 1465 | 1466 |
_writer_bits::writeToken(*_os << ' ', _attributes_caption); |
| 1466 | 1467 |
} |
| 1467 | 1468 |
*_os << std::endl; |
| 1468 | 1469 |
for (typename Attributes::iterator it = _attributes.begin(); |
| 1469 | 1470 |
it != _attributes.end(); ++it) {
|
| 1470 | 1471 |
_writer_bits::writeToken(*_os, it->first) << ' '; |
| 1471 | 1472 |
_writer_bits::writeToken(*_os, it->second->get()); |
| 1472 | 1473 |
*_os << std::endl; |
| 1473 | 1474 |
} |
| 1474 | 1475 |
} |
| 1475 | 1476 |
|
| 1476 | 1477 |
public: |
| 1477 | 1478 |
|
| 1478 | 1479 |
/// \name Execution of the writer |
| 1479 | 1480 |
/// @{
|
| 1480 | 1481 |
|
| 1481 | 1482 |
/// \brief Start the batch processing |
| 1482 | 1483 |
/// |
| 1483 | 1484 |
/// This function starts the batch processing. |
| 1484 | 1485 |
void run() {
|
| 1485 | 1486 |
if (!_skip_nodes) {
|
| 1486 | 1487 |
writeNodes(); |
| 1487 | 1488 |
} else {
|
| 1488 | 1489 |
createNodeIndex(); |
| 1489 | 1490 |
} |
| 1490 | 1491 |
if (!_skip_edges) {
|
| 1491 | 1492 |
writeEdges(); |
| 1492 | 1493 |
} else {
|
| 1493 | 1494 |
createEdgeIndex(); |
| 1494 | 1495 |
} |
| 1495 | 1496 |
writeAttributes(); |
| 1496 | 1497 |
} |
| 1497 | 1498 |
|
| 1498 | 1499 |
/// \brief Give back the stream of the writer |
| 1499 | 1500 |
/// |
| 1500 | 1501 |
/// Give back the stream of the writer |
| 1501 | 1502 |
std::ostream& ostream() {
|
| 1502 | 1503 |
return *_os; |
| 1503 | 1504 |
} |
| 1504 | 1505 |
|
| 1505 | 1506 |
/// @} |
| 1506 | 1507 |
}; |
| 1507 | 1508 |
|
| 1508 | 1509 |
/// \brief Return a \ref GraphWriter class |
| 1509 | 1510 |
/// |
| 1510 | 1511 |
/// This function just returns a \ref GraphWriter class. |
| 1511 | 1512 |
/// \relates GraphWriter |
| 1512 | 1513 |
template <typename Graph> |
| 1513 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) {
|
|
| 1514 |
GraphWriter<Graph> tmp(os, graph); |
|
| 1514 |
GraphWriter<Graph> graphWriter(const Graph& graph, |
|
| 1515 |
std::ostream& os = std::cout) {
|
|
| 1516 |
GraphWriter<Graph> tmp(graph, os); |
|
| 1515 | 1517 |
return tmp; |
| 1516 | 1518 |
} |
| 1517 | 1519 |
|
| 1518 | 1520 |
/// \brief Return a \ref GraphWriter class |
| 1519 | 1521 |
/// |
| 1520 | 1522 |
/// This function just returns a \ref GraphWriter class. |
| 1521 | 1523 |
/// \relates GraphWriter |
| 1522 | 1524 |
template <typename Graph> |
| 1523 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) {
|
|
| 1524 |
GraphWriter<Graph> tmp(fn, graph); |
|
| 1525 |
GraphWriter<Graph> graphWriter(const Graph& graph, const std::string& fn) {
|
|
| 1526 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 1525 | 1527 |
return tmp; |
| 1526 | 1528 |
} |
| 1527 | 1529 |
|
| 1528 | 1530 |
/// \brief Return a \ref GraphWriter class |
| 1529 | 1531 |
/// |
| 1530 | 1532 |
/// This function just returns a \ref GraphWriter class. |
| 1531 | 1533 |
/// \relates GraphWriter |
| 1532 | 1534 |
template <typename Graph> |
| 1533 |
GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) {
|
|
| 1534 |
GraphWriter<Graph> tmp(fn, graph); |
|
| 1535 |
GraphWriter<Graph> graphWriter(const Graph& graph, const char* fn) {
|
|
| 1536 |
GraphWriter<Graph> tmp(graph, fn); |
|
| 1535 | 1537 |
return tmp; |
| 1536 | 1538 |
} |
| 1537 | 1539 |
|
| 1538 | 1540 |
class SectionWriter; |
| 1539 | 1541 |
|
| 1540 | 1542 |
SectionWriter sectionWriter(std::istream& is); |
| 1541 | 1543 |
SectionWriter sectionWriter(const std::string& fn); |
| 1542 | 1544 |
SectionWriter sectionWriter(const char* fn); |
| 1543 | 1545 |
|
| 1544 | 1546 |
/// \ingroup lemon_io |
| 1545 | 1547 |
/// |
| 1546 | 1548 |
/// \brief Section writer class |
| 1547 | 1549 |
/// |
| 1548 | 1550 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
| 1549 | 1551 |
/// which contain any data in arbitrary format. Such sections can be |
| 1550 | 1552 |
/// written with this class. A writing rule can be added to the |
| 1551 | 1553 |
/// class with two different functions. With the \c sectionLines() |
| 1552 | 1554 |
/// function a generator can write the section line-by-line, while |
| 1553 | 1555 |
/// with the \c sectionStream() member the section can be written to |
| 1554 | 1556 |
/// an output stream. |
| 1555 | 1557 |
class SectionWriter {
|
| 1556 | 1558 |
private: |
| 1557 | 1559 |
|
| 1558 | 1560 |
std::ostream* _os; |
| 1559 | 1561 |
bool local_os; |
| 1560 | 1562 |
|
| 1561 | 1563 |
typedef std::vector<std::pair<std::string, _writer_bits::Section*> > |
| 1562 | 1564 |
Sections; |
| 1563 | 1565 |
|
| 1564 | 1566 |
Sections _sections; |
| 1565 | 1567 |
|
| 1566 | 1568 |
public: |
| 1567 | 1569 |
|
| 1568 | 1570 |
/// \brief Constructor |
| 1569 | 1571 |
/// |
| 1570 | 1572 |
/// Construct a section writer, which writes to the given output |
| 1571 | 1573 |
/// stream. |
| 1572 | 1574 |
SectionWriter(std::ostream& os) |
| 1573 | 1575 |
: _os(&os), local_os(false) {}
|
| 1574 | 1576 |
|
| 1575 | 1577 |
/// \brief Constructor |
| 1576 | 1578 |
/// |
| 1577 | 1579 |
/// Construct a section writer, which writes into the given file. |
| 1578 | 1580 |
SectionWriter(const std::string& fn) |
| 1579 | 1581 |
: _os(new std::ofstream(fn.c_str())), local_os(true) {}
|
| 1580 | 1582 |
|
| 1581 | 1583 |
/// \brief Constructor |
| 1582 | 1584 |
/// |
| 1583 | 1585 |
/// Construct a section writer, which writes into the given file. |
| 1584 | 1586 |
SectionWriter(const char* fn) |
| 1585 | 1587 |
: _os(new std::ofstream(fn)), local_os(true) {}
|
| 1586 | 1588 |
|
| 1587 | 1589 |
/// \brief Destructor |
| 1588 | 1590 |
~SectionWriter() {
|
| 1589 | 1591 |
for (Sections::iterator it = _sections.begin(); |
| 1590 | 1592 |
it != _sections.end(); ++it) {
|
| 1591 | 1593 |
delete it->second; |
| 1592 | 1594 |
} |
| 1593 | 1595 |
|
| 1594 | 1596 |
if (local_os) {
|
| 1595 | 1597 |
delete _os; |
| 1596 | 1598 |
} |
| 1597 | 1599 |
|
| 1598 | 1600 |
} |
| 1599 | 1601 |
|
| 1600 | 1602 |
private: |
| 1601 | 1603 |
|
| 1602 | 1604 |
friend SectionWriter sectionWriter(std::ostream& os); |
| 1603 | 1605 |
friend SectionWriter sectionWriter(const std::string& fn); |
| 1604 | 1606 |
friend SectionWriter sectionWriter(const char* fn); |
| 1605 | 1607 |
|
| 1606 | 1608 |
SectionWriter(SectionWriter& other) |
| 1607 | 1609 |
: _os(other._os), local_os(other.local_os) {
|
| 1608 | 1610 |
|
| 1609 | 1611 |
other._os = 0; |
| 1610 | 1612 |
other.local_os = false; |
| 1611 | 1613 |
|
| 1612 | 1614 |
_sections.swap(other._sections); |
| 1613 | 1615 |
} |
| 1614 | 1616 |
|
| 1615 | 1617 |
SectionWriter& operator=(const SectionWriter&); |
| 1616 | 1618 |
|
| 1617 | 1619 |
public: |
| 1618 | 1620 |
|
| 1619 | 1621 |
/// \name Section writers |
| 1620 | 1622 |
/// @{
|
| 1621 | 1623 |
|
| 1622 | 1624 |
/// \brief Add a section writer with line oriented writing |
| 1623 | 1625 |
/// |
| 1624 | 1626 |
/// The first parameter is the type descriptor of the section, the |
| 1625 | 1627 |
/// second is a generator with std::string values. At the writing |
| 1626 | 1628 |
/// process, the returned \c std::string will be written into the |
| 1627 | 1629 |
/// output file until it is an empty string. |
| 1628 | 1630 |
/// |
| 1629 | 1631 |
/// For example, an integer vector is written into a section. |
| 1630 | 1632 |
///\code |
| 1631 | 1633 |
/// @numbers |
| 1632 | 1634 |
/// 12 45 23 78 |
| 1633 | 1635 |
/// 4 28 38 28 |
| 1634 | 1636 |
/// 23 6 16 |
| 1635 | 1637 |
///\endcode |
| 1636 | 1638 |
/// |
| 1637 | 1639 |
/// The generator is implemented as a struct. |
| 1638 | 1640 |
///\code |
| 1639 | 1641 |
/// struct NumberSection {
|
| 1640 | 1642 |
/// std::vector<int>::const_iterator _it, _end; |
| 1641 | 1643 |
/// NumberSection(const std::vector<int>& data) |
| 1642 | 1644 |
/// : _it(data.begin()), _end(data.end()) {}
|
| 1643 | 1645 |
/// std::string operator()() {
|
| 1644 | 1646 |
/// int rem_in_line = 4; |
| 1645 | 1647 |
/// std::ostringstream ls; |
| 1646 | 1648 |
/// while (rem_in_line > 0 && _it != _end) {
|
| 1647 | 1649 |
/// ls << *(_it++) << ' '; |
| 1648 | 1650 |
/// --rem_in_line; |
| 1649 | 1651 |
/// } |
| 1650 | 1652 |
/// return ls.str(); |
| 1651 | 1653 |
/// } |
| 1652 | 1654 |
/// }; |
| 1653 | 1655 |
/// |
| 1654 | 1656 |
/// // ... |
| 1655 | 1657 |
/// |
| 1656 | 1658 |
/// writer.sectionLines("numbers", NumberSection(vec));
|
| 1657 | 1659 |
///\endcode |
| 1658 | 1660 |
template <typename Functor> |
| 1659 | 1661 |
SectionWriter& sectionLines(const std::string& type, Functor functor) {
|
| 1660 | 1662 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1661 | 1663 |
_sections.push_back(std::make_pair(type, |
| 1662 | 1664 |
new _writer_bits::LineSection<Functor>(functor))); |
| 1663 | 1665 |
return *this; |
| 1664 | 1666 |
} |
| 1665 | 1667 |
|
| 1666 | 1668 |
|
| 1667 | 1669 |
/// \brief Add a section writer with stream oriented writing |
| 1668 | 1670 |
/// |
| 1669 | 1671 |
/// The first parameter is the type of the section, the second is |
| 1670 | 1672 |
/// a functor, which takes a \c std::ostream& parameter. The |
| 1671 | 1673 |
/// functor writes the section to the output stream. |
| 1672 | 1674 |
/// \warning The last line must be closed with end-line character. |
| 1673 | 1675 |
template <typename Functor> |
| 1674 | 1676 |
SectionWriter& sectionStream(const std::string& type, Functor functor) {
|
| 1675 | 1677 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
| 1676 | 1678 |
_sections.push_back(std::make_pair(type, |
| 1677 | 1679 |
new _writer_bits::StreamSection<Functor>(functor))); |
| 1678 | 1680 |
return *this; |
| 1679 | 1681 |
} |
| 1680 | 1682 |
|
| 1681 | 1683 |
/// @} |
| 1682 | 1684 |
|
| 1683 | 1685 |
public: |
| 1684 | 1686 |
|
| 1685 | 1687 |
|
| 1686 | 1688 |
/// \name Execution of the writer |
| 1687 | 1689 |
/// @{
|
| 1688 | 1690 |
|
| 1689 | 1691 |
/// \brief Start the batch processing |
| 1690 | 1692 |
/// |
| 1691 | 1693 |
/// This function starts the batch processing. |
| 1692 | 1694 |
void run() {
|
| 1693 | 1695 |
|
| 1694 | 1696 |
LEMON_ASSERT(_os != 0, "This writer is assigned to an other writer"); |
| 1695 | 1697 |
|
| 1696 | 1698 |
for (Sections::iterator it = _sections.begin(); |
| 1697 | 1699 |
it != _sections.end(); ++it) {
|
| 1698 | 1700 |
(*_os) << '@' << it->first << std::endl; |
| 1699 | 1701 |
it->second->process(*_os); |
| 1700 | 1702 |
} |
| 1701 | 1703 |
} |
| 1702 | 1704 |
|
| 1703 | 1705 |
/// \brief Give back the stream of the writer |
| 1704 | 1706 |
/// |
| 1705 | 1707 |
/// Returns the stream of the writer |
| 1706 | 1708 |
std::ostream& ostream() {
|
| 1707 | 1709 |
return *_os; |
| 1708 | 1710 |
} |
| 1709 | 1711 |
|
| 1710 | 1712 |
/// @} |
| 1711 | 1713 |
|
| 1712 | 1714 |
}; |
| 1713 | 1715 |
|
| 1714 | 1716 |
/// \brief Return a \ref SectionWriter class |
| 1715 | 1717 |
/// |
| 1716 | 1718 |
/// This function just returns a \ref SectionWriter class. |
| 1717 | 1719 |
/// \relates SectionWriter |
| 1718 | 1720 |
inline SectionWriter sectionWriter(std::ostream& os) {
|
| 1719 | 1721 |
SectionWriter tmp(os); |
| 1720 | 1722 |
return tmp; |
| 1721 | 1723 |
} |
| 1722 | 1724 |
|
| 1723 | 1725 |
/// \brief Return a \ref SectionWriter class |
| 1724 | 1726 |
/// |
| 1725 | 1727 |
/// This function just returns a \ref SectionWriter class. |
| 1726 | 1728 |
/// \relates SectionWriter |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/bfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"@arcs\n" |
| 41 | 41 |
" label\n" |
| 42 | 42 |
"0 1 0\n" |
| 43 | 43 |
"1 2 1\n" |
| 44 | 44 |
"2 3 2\n" |
| 45 | 45 |
"3 4 3\n" |
| 46 | 46 |
"0 3 4\n" |
| 47 | 47 |
"0 3 5\n" |
| 48 | 48 |
"5 2 6\n" |
| 49 | 49 |
"@attributes\n" |
| 50 | 50 |
"source 0\n" |
| 51 | 51 |
"target 4\n"; |
| 52 | 52 |
|
| 53 | 53 |
void checkBfsCompile() |
| 54 | 54 |
{
|
| 55 | 55 |
typedef concepts::Digraph Digraph; |
| 56 | 56 |
typedef Bfs<Digraph> BType; |
| 57 | 57 |
typedef Digraph::Node Node; |
| 58 | 58 |
typedef Digraph::Arc Arc; |
| 59 | 59 |
|
| 60 | 60 |
Digraph G; |
| 61 | 61 |
Node s, t; |
| 62 | 62 |
Arc e; |
| 63 | 63 |
int l; |
| 64 | 64 |
bool b; |
| 65 | 65 |
BType::DistMap d(G); |
| 66 | 66 |
BType::PredMap p(G); |
| 67 | 67 |
Path<Digraph> pp; |
| 68 | 68 |
|
| 69 | 69 |
{
|
| 70 | 70 |
BType bfs_test(G); |
| 71 | 71 |
|
| 72 | 72 |
bfs_test.run(s); |
| 73 | 73 |
bfs_test.run(s,t); |
| 74 | 74 |
bfs_test.run(); |
| 75 | 75 |
|
| 76 | 76 |
l = bfs_test.dist(t); |
| 77 | 77 |
e = bfs_test.predArc(t); |
| 78 | 78 |
s = bfs_test.predNode(t); |
| 79 | 79 |
b = bfs_test.reached(t); |
| 80 | 80 |
d = bfs_test.distMap(); |
| 81 | 81 |
p = bfs_test.predMap(); |
| 82 | 82 |
pp = bfs_test.path(t); |
| 83 | 83 |
} |
| 84 | 84 |
{
|
| 85 | 85 |
BType |
| 86 | 86 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 87 | 87 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 88 | 88 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 89 | 89 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 90 | 90 |
::SetStandardProcessedMap |
| 91 | 91 |
::Create bfs_test(G); |
| 92 | 92 |
|
| 93 | 93 |
bfs_test.run(s); |
| 94 | 94 |
bfs_test.run(s,t); |
| 95 | 95 |
bfs_test.run(); |
| 96 | 96 |
|
| 97 | 97 |
l = bfs_test.dist(t); |
| 98 | 98 |
e = bfs_test.predArc(t); |
| 99 | 99 |
s = bfs_test.predNode(t); |
| 100 | 100 |
b = bfs_test.reached(t); |
| 101 | 101 |
pp = bfs_test.path(t); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
void checkBfsFunctionCompile() |
| 106 | 106 |
{
|
| 107 | 107 |
typedef int VType; |
| 108 | 108 |
typedef concepts::Digraph Digraph; |
| 109 | 109 |
typedef Digraph::Arc Arc; |
| 110 | 110 |
typedef Digraph::Node Node; |
| 111 | 111 |
|
| 112 | 112 |
Digraph g; |
| 113 | 113 |
bool b; |
| 114 | 114 |
bfs(g).run(Node()); |
| 115 | 115 |
b=bfs(g).run(Node(),Node()); |
| 116 | 116 |
bfs(g).run(); |
| 117 | 117 |
bfs(g) |
| 118 | 118 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 119 | 119 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 120 | 120 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 121 | 121 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 122 | 122 |
.run(Node()); |
| 123 | 123 |
b=bfs(g) |
| 124 | 124 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 125 | 125 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 126 | 126 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 127 | 127 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 128 | 128 |
.path(concepts::Path<Digraph>()) |
| 129 | 129 |
.dist(VType()) |
| 130 | 130 |
.run(Node(),Node()); |
| 131 | 131 |
bfs(g) |
| 132 | 132 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 133 | 133 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 134 | 134 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 135 | 135 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 136 | 136 |
.run(); |
| 137 | 137 |
} |
| 138 | 138 |
|
| 139 | 139 |
template <class Digraph> |
| 140 | 140 |
void checkBfs() {
|
| 141 | 141 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 142 | 142 |
|
| 143 | 143 |
Digraph G; |
| 144 | 144 |
Node s, t; |
| 145 | 145 |
|
| 146 | 146 |
std::istringstream input(test_lgf); |
| 147 |
digraphReader( |
|
| 147 |
digraphReader(G, input). |
|
| 148 | 148 |
node("source", s).
|
| 149 | 149 |
node("target", t).
|
| 150 | 150 |
run(); |
| 151 | 151 |
|
| 152 | 152 |
Bfs<Digraph> bfs_test(G); |
| 153 | 153 |
bfs_test.run(s); |
| 154 | 154 |
|
| 155 | 155 |
check(bfs_test.dist(t)==2,"Bfs found a wrong path."); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = bfs_test.path(t); |
| 158 | 158 |
check(p.length()==2,"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
|
| 164 | 164 |
for(ArcIt a(G); a!=INVALID; ++a) {
|
| 165 | 165 |
Node u=G.source(a); |
| 166 | 166 |
Node v=G.target(a); |
| 167 | 167 |
check( !bfs_test.reached(u) || |
| 168 | 168 |
(bfs_test.dist(v) <= bfs_test.dist(u)+1), |
| 169 | 169 |
"Wrong output. " << G.id(u) << "->" << G.id(v)); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 173 | 173 |
if (bfs_test.reached(v)) {
|
| 174 | 174 |
check(v==s || bfs_test.predArc(v)!=INVALID, "Wrong tree."); |
| 175 | 175 |
if (bfs_test.predArc(v)!=INVALID ) {
|
| 176 | 176 |
Arc a=bfs_test.predArc(v); |
| 177 | 177 |
Node u=G.source(a); |
| 178 | 178 |
check(u==bfs_test.predNode(v),"Wrong tree."); |
| 179 | 179 |
check(bfs_test.dist(v) - bfs_test.dist(u) == 1, |
| 180 | 180 |
"Wrong distance. Difference: " |
| 181 | 181 |
<< std::abs(bfs_test.dist(v) - bfs_test.dist(u) - 1)); |
| 182 | 182 |
} |
| 183 | 183 |
} |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
{
|
| 187 | 187 |
NullMap<Node,Arc> myPredMap; |
| 188 | 188 |
bfs(G).predMap(myPredMap).run(s); |
| 189 | 189 |
} |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
int main() |
| 193 | 193 |
{
|
| 194 | 194 |
checkBfs<ListDigraph>(); |
| 195 | 195 |
checkBfs<SmartDigraph>(); |
| 196 | 196 |
return 0; |
| 197 | 197 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dfs.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
|
| 26 | 26 |
#include "graph_test.h" |
| 27 | 27 |
#include "test_tools.h" |
| 28 | 28 |
|
| 29 | 29 |
using namespace lemon; |
| 30 | 30 |
|
| 31 | 31 |
char test_lgf[] = |
| 32 | 32 |
"@nodes\n" |
| 33 | 33 |
"label\n" |
| 34 | 34 |
"0\n" |
| 35 | 35 |
"1\n" |
| 36 | 36 |
"2\n" |
| 37 | 37 |
"3\n" |
| 38 | 38 |
"4\n" |
| 39 | 39 |
"5\n" |
| 40 | 40 |
"6\n" |
| 41 | 41 |
"@arcs\n" |
| 42 | 42 |
" label\n" |
| 43 | 43 |
"0 1 0\n" |
| 44 | 44 |
"1 2 1\n" |
| 45 | 45 |
"2 3 2\n" |
| 46 | 46 |
"1 4 3\n" |
| 47 | 47 |
"4 2 4\n" |
| 48 | 48 |
"4 5 5\n" |
| 49 | 49 |
"5 0 6\n" |
| 50 | 50 |
"6 3 7\n" |
| 51 | 51 |
"@attributes\n" |
| 52 | 52 |
"source 0\n" |
| 53 | 53 |
"target 5\n"; |
| 54 | 54 |
|
| 55 | 55 |
void checkDfsCompile() |
| 56 | 56 |
{
|
| 57 | 57 |
typedef concepts::Digraph Digraph; |
| 58 | 58 |
typedef Dfs<Digraph> DType; |
| 59 | 59 |
typedef Digraph::Node Node; |
| 60 | 60 |
typedef Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
Digraph G; |
| 63 | 63 |
Node s, t; |
| 64 | 64 |
Arc e; |
| 65 | 65 |
int l; |
| 66 | 66 |
bool b; |
| 67 | 67 |
DType::DistMap d(G); |
| 68 | 68 |
DType::PredMap p(G); |
| 69 | 69 |
Path<Digraph> pp; |
| 70 | 70 |
|
| 71 | 71 |
{
|
| 72 | 72 |
DType dfs_test(G); |
| 73 | 73 |
|
| 74 | 74 |
dfs_test.run(s); |
| 75 | 75 |
dfs_test.run(s,t); |
| 76 | 76 |
dfs_test.run(); |
| 77 | 77 |
|
| 78 | 78 |
l = dfs_test.dist(t); |
| 79 | 79 |
e = dfs_test.predArc(t); |
| 80 | 80 |
s = dfs_test.predNode(t); |
| 81 | 81 |
b = dfs_test.reached(t); |
| 82 | 82 |
d = dfs_test.distMap(); |
| 83 | 83 |
p = dfs_test.predMap(); |
| 84 | 84 |
pp = dfs_test.path(t); |
| 85 | 85 |
} |
| 86 | 86 |
{
|
| 87 | 87 |
DType |
| 88 | 88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 89 | 89 |
::SetDistMap<concepts::ReadWriteMap<Node,int> > |
| 90 | 90 |
::SetReachedMap<concepts::ReadWriteMap<Node,bool> > |
| 91 | 91 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 92 | 92 |
::SetStandardProcessedMap |
| 93 | 93 |
::Create dfs_test(G); |
| 94 | 94 |
|
| 95 | 95 |
dfs_test.run(s); |
| 96 | 96 |
dfs_test.run(s,t); |
| 97 | 97 |
dfs_test.run(); |
| 98 | 98 |
|
| 99 | 99 |
l = dfs_test.dist(t); |
| 100 | 100 |
e = dfs_test.predArc(t); |
| 101 | 101 |
s = dfs_test.predNode(t); |
| 102 | 102 |
b = dfs_test.reached(t); |
| 103 | 103 |
pp = dfs_test.path(t); |
| 104 | 104 |
} |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
void checkDfsFunctionCompile() |
| 108 | 108 |
{
|
| 109 | 109 |
typedef int VType; |
| 110 | 110 |
typedef concepts::Digraph Digraph; |
| 111 | 111 |
typedef Digraph::Arc Arc; |
| 112 | 112 |
typedef Digraph::Node Node; |
| 113 | 113 |
|
| 114 | 114 |
Digraph g; |
| 115 | 115 |
bool b; |
| 116 | 116 |
dfs(g).run(Node()); |
| 117 | 117 |
b=dfs(g).run(Node(),Node()); |
| 118 | 118 |
dfs(g).run(); |
| 119 | 119 |
dfs(g) |
| 120 | 120 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 121 | 121 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 122 | 122 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 123 | 123 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 124 | 124 |
.run(Node()); |
| 125 | 125 |
b=dfs(g) |
| 126 | 126 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 127 | 127 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 128 | 128 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 129 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 130 | 130 |
.path(concepts::Path<Digraph>()) |
| 131 | 131 |
.dist(VType()) |
| 132 | 132 |
.run(Node(),Node()); |
| 133 | 133 |
dfs(g) |
| 134 | 134 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 135 | 135 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 136 | 136 |
.reachedMap(concepts::ReadWriteMap<Node,bool>()) |
| 137 | 137 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 138 | 138 |
.run(); |
| 139 | 139 |
} |
| 140 | 140 |
|
| 141 | 141 |
template <class Digraph> |
| 142 | 142 |
void checkDfs() {
|
| 143 | 143 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 144 | 144 |
|
| 145 | 145 |
Digraph G; |
| 146 | 146 |
Node s, t; |
| 147 | 147 |
|
| 148 | 148 |
std::istringstream input(test_lgf); |
| 149 |
digraphReader( |
|
| 149 |
digraphReader(G, input). |
|
| 150 | 150 |
node("source", s).
|
| 151 | 151 |
node("target", t).
|
| 152 | 152 |
run(); |
| 153 | 153 |
|
| 154 | 154 |
Dfs<Digraph> dfs_test(G); |
| 155 | 155 |
dfs_test.run(s); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = dfs_test.path(t); |
| 158 | 158 |
check(p.length() == dfs_test.dist(t),"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 164 | 164 |
if (dfs_test.reached(v)) {
|
| 165 | 165 |
check(v==s || dfs_test.predArc(v)!=INVALID, "Wrong tree."); |
| 166 | 166 |
if (dfs_test.predArc(v)!=INVALID ) {
|
| 167 | 167 |
Arc e=dfs_test.predArc(v); |
| 168 | 168 |
Node u=G.source(e); |
| 169 | 169 |
check(u==dfs_test.predNode(v),"Wrong tree."); |
| 170 | 170 |
check(dfs_test.dist(v) - dfs_test.dist(u) == 1, |
| 171 | 171 |
"Wrong distance. (" << dfs_test.dist(u) << "->"
|
| 172 | 172 |
<< dfs_test.dist(v) << ")"); |
| 173 | 173 |
} |
| 174 | 174 |
} |
| 175 | 175 |
} |
| 176 | 176 |
|
| 177 | 177 |
{
|
| 178 | 178 |
NullMap<Node,Arc> myPredMap; |
| 179 | 179 |
dfs(G).predMap(myPredMap).run(s); |
| 180 | 180 |
} |
| 181 | 181 |
} |
| 182 | 182 |
|
| 183 | 183 |
int main() |
| 184 | 184 |
{
|
| 185 | 185 |
checkDfs<ListDigraph>(); |
| 186 | 186 |
checkDfs<SmartDigraph>(); |
| 187 | 187 |
return 0; |
| 188 | 188 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 |
#include <lemon/concepts/digraph.h> |
| 20 | 20 |
#include <lemon/smart_graph.h> |
| 21 | 21 |
#include <lemon/list_graph.h> |
| 22 | 22 |
#include <lemon/lgf_reader.h> |
| 23 | 23 |
#include <lemon/dijkstra.h> |
| 24 | 24 |
#include <lemon/path.h> |
| 25 | 25 |
#include <lemon/bin_heap.h> |
| 26 | 26 |
|
| 27 | 27 |
#include "graph_test.h" |
| 28 | 28 |
#include "test_tools.h" |
| 29 | 29 |
|
| 30 | 30 |
using namespace lemon; |
| 31 | 31 |
|
| 32 | 32 |
char test_lgf[] = |
| 33 | 33 |
"@nodes\n" |
| 34 | 34 |
"label\n" |
| 35 | 35 |
"0\n" |
| 36 | 36 |
"1\n" |
| 37 | 37 |
"2\n" |
| 38 | 38 |
"3\n" |
| 39 | 39 |
"4\n" |
| 40 | 40 |
"@arcs\n" |
| 41 | 41 |
" label length\n" |
| 42 | 42 |
"0 1 0 1\n" |
| 43 | 43 |
"1 2 1 1\n" |
| 44 | 44 |
"2 3 2 1\n" |
| 45 | 45 |
"0 3 4 5\n" |
| 46 | 46 |
"0 3 5 10\n" |
| 47 | 47 |
"0 3 6 7\n" |
| 48 | 48 |
"4 2 7 1\n" |
| 49 | 49 |
"@attributes\n" |
| 50 | 50 |
"source 0\n" |
| 51 | 51 |
"target 3\n"; |
| 52 | 52 |
|
| 53 | 53 |
void checkDijkstraCompile() |
| 54 | 54 |
{
|
| 55 | 55 |
typedef int VType; |
| 56 | 56 |
typedef concepts::Digraph Digraph; |
| 57 | 57 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
| 58 | 58 |
typedef Dijkstra<Digraph, LengthMap> DType; |
| 59 | 59 |
typedef Digraph::Node Node; |
| 60 | 60 |
typedef Digraph::Arc Arc; |
| 61 | 61 |
|
| 62 | 62 |
Digraph G; |
| 63 | 63 |
Node s, t; |
| 64 | 64 |
Arc e; |
| 65 | 65 |
VType l; |
| 66 | 66 |
bool b; |
| 67 | 67 |
DType::DistMap d(G); |
| 68 | 68 |
DType::PredMap p(G); |
| 69 | 69 |
LengthMap length; |
| 70 | 70 |
Path<Digraph> pp; |
| 71 | 71 |
|
| 72 | 72 |
{
|
| 73 | 73 |
DType dijkstra_test(G,length); |
| 74 | 74 |
|
| 75 | 75 |
dijkstra_test.run(s); |
| 76 | 76 |
dijkstra_test.run(s,t); |
| 77 | 77 |
|
| 78 | 78 |
l = dijkstra_test.dist(t); |
| 79 | 79 |
e = dijkstra_test.predArc(t); |
| 80 | 80 |
s = dijkstra_test.predNode(t); |
| 81 | 81 |
b = dijkstra_test.reached(t); |
| 82 | 82 |
d = dijkstra_test.distMap(); |
| 83 | 83 |
p = dijkstra_test.predMap(); |
| 84 | 84 |
pp = dijkstra_test.path(t); |
| 85 | 85 |
} |
| 86 | 86 |
{
|
| 87 | 87 |
DType |
| 88 | 88 |
::SetPredMap<concepts::ReadWriteMap<Node,Arc> > |
| 89 | 89 |
::SetDistMap<concepts::ReadWriteMap<Node,VType> > |
| 90 | 90 |
::SetProcessedMap<concepts::WriteMap<Node,bool> > |
| 91 | 91 |
::SetStandardProcessedMap |
| 92 | 92 |
::SetOperationTraits<DijkstraWidestPathOperationTraits<VType> > |
| 93 | 93 |
::SetHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 94 | 94 |
::SetStandardHeap<BinHeap<VType, concepts::ReadWriteMap<Node,int> > > |
| 95 | 95 |
::Create dijkstra_test(G,length); |
| 96 | 96 |
|
| 97 | 97 |
dijkstra_test.run(s); |
| 98 | 98 |
dijkstra_test.run(s,t); |
| 99 | 99 |
|
| 100 | 100 |
l = dijkstra_test.dist(t); |
| 101 | 101 |
e = dijkstra_test.predArc(t); |
| 102 | 102 |
s = dijkstra_test.predNode(t); |
| 103 | 103 |
b = dijkstra_test.reached(t); |
| 104 | 104 |
pp = dijkstra_test.path(t); |
| 105 | 105 |
} |
| 106 | 106 |
|
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
void checkDijkstraFunctionCompile() |
| 110 | 110 |
{
|
| 111 | 111 |
typedef int VType; |
| 112 | 112 |
typedef concepts::Digraph Digraph; |
| 113 | 113 |
typedef Digraph::Arc Arc; |
| 114 | 114 |
typedef Digraph::Node Node; |
| 115 | 115 |
typedef concepts::ReadMap<Digraph::Arc,VType> LengthMap; |
| 116 | 116 |
|
| 117 | 117 |
Digraph g; |
| 118 | 118 |
bool b; |
| 119 | 119 |
dijkstra(g,LengthMap()).run(Node()); |
| 120 | 120 |
b=dijkstra(g,LengthMap()).run(Node(),Node()); |
| 121 | 121 |
dijkstra(g,LengthMap()) |
| 122 | 122 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 123 | 123 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 124 | 124 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 125 | 125 |
.run(Node()); |
| 126 | 126 |
b=dijkstra(g,LengthMap()) |
| 127 | 127 |
.predMap(concepts::ReadWriteMap<Node,Arc>()) |
| 128 | 128 |
.distMap(concepts::ReadWriteMap<Node,VType>()) |
| 129 | 129 |
.processedMap(concepts::WriteMap<Node,bool>()) |
| 130 | 130 |
.path(concepts::Path<Digraph>()) |
| 131 | 131 |
.dist(VType()) |
| 132 | 132 |
.run(Node(),Node()); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
template <class Digraph> |
| 136 | 136 |
void checkDijkstra() {
|
| 137 | 137 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
| 138 | 138 |
typedef typename Digraph::template ArcMap<int> LengthMap; |
| 139 | 139 |
|
| 140 | 140 |
Digraph G; |
| 141 | 141 |
Node s, t; |
| 142 | 142 |
LengthMap length(G); |
| 143 | 143 |
|
| 144 | 144 |
std::istringstream input(test_lgf); |
| 145 |
digraphReader( |
|
| 145 |
digraphReader(G, input). |
|
| 146 | 146 |
arcMap("length", length).
|
| 147 | 147 |
node("source", s).
|
| 148 | 148 |
node("target", t).
|
| 149 | 149 |
run(); |
| 150 | 150 |
|
| 151 | 151 |
Dijkstra<Digraph, LengthMap> |
| 152 | 152 |
dijkstra_test(G, length); |
| 153 | 153 |
dijkstra_test.run(s); |
| 154 | 154 |
|
| 155 | 155 |
check(dijkstra_test.dist(t)==3,"Dijkstra found a wrong path."); |
| 156 | 156 |
|
| 157 | 157 |
Path<Digraph> p = dijkstra_test.path(t); |
| 158 | 158 |
check(p.length()==3,"path() found a wrong path."); |
| 159 | 159 |
check(checkPath(G, p),"path() found a wrong path."); |
| 160 | 160 |
check(pathSource(G, p) == s,"path() found a wrong path."); |
| 161 | 161 |
check(pathTarget(G, p) == t,"path() found a wrong path."); |
| 162 | 162 |
|
| 163 | 163 |
for(ArcIt e(G); e!=INVALID; ++e) {
|
| 164 | 164 |
Node u=G.source(e); |
| 165 | 165 |
Node v=G.target(e); |
| 166 | 166 |
check( !dijkstra_test.reached(u) || |
| 167 | 167 |
(dijkstra_test.dist(v) - dijkstra_test.dist(u) <= length[e]), |
| 168 | 168 |
"Wrong output. dist(target)-dist(source)-arc_length=" << |
| 169 | 169 |
dijkstra_test.dist(v) - dijkstra_test.dist(u) - length[e]); |
| 170 | 170 |
} |
| 171 | 171 |
|
| 172 | 172 |
for(NodeIt v(G); v!=INVALID; ++v) {
|
| 173 | 173 |
if (dijkstra_test.reached(v)) {
|
| 174 | 174 |
check(v==s || dijkstra_test.predArc(v)!=INVALID, "Wrong tree."); |
| 175 | 175 |
if (dijkstra_test.predArc(v)!=INVALID ) {
|
| 176 | 176 |
Arc e=dijkstra_test.predArc(v); |
| 177 | 177 |
Node u=G.source(e); |
| 178 | 178 |
check(u==dijkstra_test.predNode(v),"Wrong tree."); |
| 179 | 179 |
check(dijkstra_test.dist(v) - dijkstra_test.dist(u) == length[e], |
| 180 | 180 |
"Wrong distance! Difference: " << |
| 181 | 181 |
std::abs(dijkstra_test.dist(v)-dijkstra_test.dist(u)-length[e])); |
| 182 | 182 |
} |
| 183 | 183 |
} |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
{
|
| 187 | 187 |
NullMap<Node,Arc> myPredMap; |
| 188 | 188 |
dijkstra(G,length).predMap(myPredMap).run(s); |
| 189 | 189 |
} |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
int main() {
|
| 193 | 193 |
checkDijkstra<ListDigraph>(); |
| 194 | 194 |
checkDijkstra<SmartDigraph>(); |
| 195 | 195 |
return 0; |
| 196 | 196 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 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 |
#include <iostream> |
| 20 | 20 |
#include <fstream> |
| 21 | 21 |
#include <string> |
| 22 | 22 |
#include <vector> |
| 23 | 23 |
|
| 24 | 24 |
#include <lemon/concept_check.h> |
| 25 | 25 |
#include <lemon/concepts/heap.h> |
| 26 | 26 |
|
| 27 | 27 |
#include <lemon/smart_graph.h> |
| 28 | 28 |
|
| 29 | 29 |
#include <lemon/lgf_reader.h> |
| 30 | 30 |
#include <lemon/dijkstra.h> |
| 31 | 31 |
#include <lemon/maps.h> |
| 32 | 32 |
|
| 33 | 33 |
#include <lemon/bin_heap.h> |
| 34 | 34 |
|
| 35 | 35 |
#include "test_tools.h" |
| 36 | 36 |
|
| 37 | 37 |
using namespace lemon; |
| 38 | 38 |
using namespace lemon::concepts; |
| 39 | 39 |
|
| 40 | 40 |
typedef ListDigraph Digraph; |
| 41 | 41 |
DIGRAPH_TYPEDEFS(Digraph); |
| 42 | 42 |
|
| 43 | 43 |
char test_lgf[] = |
| 44 | 44 |
"@nodes\n" |
| 45 | 45 |
"label\n" |
| 46 | 46 |
"0\n" |
| 47 | 47 |
"1\n" |
| 48 | 48 |
"2\n" |
| 49 | 49 |
"3\n" |
| 50 | 50 |
"4\n" |
| 51 | 51 |
"5\n" |
| 52 | 52 |
"6\n" |
| 53 | 53 |
"7\n" |
| 54 | 54 |
"8\n" |
| 55 | 55 |
"9\n" |
| 56 | 56 |
"@arcs\n" |
| 57 | 57 |
" label capacity\n" |
| 58 | 58 |
"0 5 0 94\n" |
| 59 | 59 |
"3 9 1 11\n" |
| 60 | 60 |
"8 7 2 83\n" |
| 61 | 61 |
"1 2 3 94\n" |
| 62 | 62 |
"5 7 4 35\n" |
| 63 | 63 |
"7 4 5 84\n" |
| 64 | 64 |
"9 5 6 38\n" |
| 65 | 65 |
"0 4 7 96\n" |
| 66 | 66 |
"6 7 8 6\n" |
| 67 | 67 |
"3 1 9 27\n" |
| 68 | 68 |
"5 2 10 77\n" |
| 69 | 69 |
"5 6 11 69\n" |
| 70 | 70 |
"6 5 12 41\n" |
| 71 | 71 |
"4 6 13 70\n" |
| 72 | 72 |
"3 2 14 45\n" |
| 73 | 73 |
"7 9 15 93\n" |
| 74 | 74 |
"5 9 16 50\n" |
| 75 | 75 |
"9 0 17 94\n" |
| 76 | 76 |
"9 6 18 67\n" |
| 77 | 77 |
"0 9 19 86\n" |
| 78 | 78 |
"@attributes\n" |
| 79 | 79 |
"source 3\n"; |
| 80 | 80 |
|
| 81 | 81 |
int test_seq[] = { 2, 28, 19, 27, 33, 25, 13, 41, 10, 26, 1, 9, 4, 34};
|
| 82 | 82 |
int test_inc[] = {20, 28, 34, 16, 0, 46, 44, 0, 42, 32, 14, 8, 6, 37};
|
| 83 | 83 |
|
| 84 | 84 |
int test_len = sizeof(test_seq) / sizeof(test_seq[0]); |
| 85 | 85 |
|
| 86 | 86 |
template <typename Heap> |
| 87 | 87 |
void heapSortTest() {
|
| 88 | 88 |
RangeMap<int> map(test_len, -1); |
| 89 | 89 |
|
| 90 | 90 |
Heap heap(map); |
| 91 | 91 |
|
| 92 | 92 |
std::vector<int> v(test_len); |
| 93 | 93 |
|
| 94 | 94 |
for (int i = 0; i < test_len; ++i) {
|
| 95 | 95 |
v[i] = test_seq[i]; |
| 96 | 96 |
heap.push(i, v[i]); |
| 97 | 97 |
} |
| 98 | 98 |
std::sort(v.begin(), v.end()); |
| 99 | 99 |
for (int i = 0; i < test_len; ++i) {
|
| 100 | 100 |
check(v[i] == heap.prio() ,"Wrong order in heap sort."); |
| 101 | 101 |
heap.pop(); |
| 102 | 102 |
} |
| 103 | 103 |
} |
| 104 | 104 |
|
| 105 | 105 |
template <typename Heap> |
| 106 | 106 |
void heapIncreaseTest() {
|
| 107 | 107 |
RangeMap<int> map(test_len, -1); |
| 108 | 108 |
|
| 109 | 109 |
Heap heap(map); |
| 110 | 110 |
|
| 111 | 111 |
std::vector<int> v(test_len); |
| 112 | 112 |
|
| 113 | 113 |
for (int i = 0; i < test_len; ++i) {
|
| 114 | 114 |
v[i] = test_seq[i]; |
| 115 | 115 |
heap.push(i, v[i]); |
| 116 | 116 |
} |
| 117 | 117 |
for (int i = 0; i < test_len; ++i) {
|
| 118 | 118 |
v[i] += test_inc[i]; |
| 119 | 119 |
heap.increase(i, v[i]); |
| 120 | 120 |
} |
| 121 | 121 |
std::sort(v.begin(), v.end()); |
| 122 | 122 |
for (int i = 0; i < test_len; ++i) {
|
| 123 | 123 |
check(v[i] == heap.prio() ,"Wrong order in heap increase test."); |
| 124 | 124 |
heap.pop(); |
| 125 | 125 |
} |
| 126 | 126 |
} |
| 127 | 127 |
|
| 128 | 128 |
|
| 129 | 129 |
|
| 130 | 130 |
template <typename Heap> |
| 131 | 131 |
void dijkstraHeapTest(const Digraph& digraph, const IntArcMap& length, |
| 132 | 132 |
Node source) {
|
| 133 | 133 |
|
| 134 | 134 |
typename Dijkstra<Digraph, IntArcMap>::template SetStandardHeap<Heap>:: |
| 135 | 135 |
Create dijkstra(digraph, length); |
| 136 | 136 |
|
| 137 | 137 |
dijkstra.run(source); |
| 138 | 138 |
|
| 139 | 139 |
for(ArcIt a(digraph); a != INVALID; ++a) {
|
| 140 | 140 |
Node s = digraph.source(a); |
| 141 | 141 |
Node t = digraph.target(a); |
| 142 | 142 |
if (dijkstra.reached(s)) {
|
| 143 | 143 |
check( dijkstra.dist(t) - dijkstra.dist(s) <= length[a], |
| 144 | 144 |
"Error in a shortest path tree!"); |
| 145 | 145 |
} |
| 146 | 146 |
} |
| 147 | 147 |
|
| 148 | 148 |
for(NodeIt n(digraph); n != INVALID; ++n) {
|
| 149 | 149 |
if ( dijkstra.reached(n) && dijkstra.predArc(n) != INVALID ) {
|
| 150 | 150 |
Arc a = dijkstra.predArc(n); |
| 151 | 151 |
Node s = digraph.source(a); |
| 152 | 152 |
check( dijkstra.dist(n) - dijkstra.dist(s) == length[a], |
| 153 | 153 |
"Error in a shortest path tree!"); |
| 154 | 154 |
} |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
} |
| 158 | 158 |
|
| 159 | 159 |
int main() {
|
| 160 | 160 |
|
| 161 | 161 |
typedef int Item; |
| 162 | 162 |
typedef int Prio; |
| 163 | 163 |
typedef RangeMap<int> ItemIntMap; |
| 164 | 164 |
|
| 165 | 165 |
Digraph digraph; |
| 166 | 166 |
IntArcMap length(digraph); |
| 167 | 167 |
Node source; |
| 168 | 168 |
|
| 169 | 169 |
std::istringstream input(test_lgf); |
| 170 |
digraphReader( |
|
| 170 |
digraphReader(digraph, input). |
|
| 171 | 171 |
arcMap("capacity", length).
|
| 172 | 172 |
node("source", source).
|
| 173 | 173 |
run(); |
| 174 | 174 |
|
| 175 | 175 |
{
|
| 176 | 176 |
typedef BinHeap<Prio, ItemIntMap> IntHeap; |
| 177 | 177 |
checkConcept<Heap<Prio, ItemIntMap>, IntHeap>(); |
| 178 | 178 |
heapSortTest<IntHeap>(); |
| 179 | 179 |
heapIncreaseTest<IntHeap>(); |
| 180 | 180 |
|
| 181 | 181 |
typedef BinHeap<Prio, IntNodeMap > NodeHeap; |
| 182 | 182 |
checkConcept<Heap<Prio, IntNodeMap >, NodeHeap>(); |
| 183 | 183 |
dijkstraHeapTest<NodeHeap>(digraph, length, source); |
| 184 | 184 |
} |
| 185 | 185 |
|
| 186 | 186 |
return 0; |
| 187 | 187 |
} |
0 comments (0 inline)