0
7
0
39
37
47
45
| ... | ... |
@@ -23,48 +23,48 @@ |
| 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 |
} |
| ... | ... |
@@ -369,78 +369,79 @@ |
| 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 |
| ... | ... |
@@ -478,102 +479,102 @@ |
| 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 |
|
| ... | ... |
@@ -1159,85 +1160,87 @@ |
| 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: |
| ... | ... |
@@ -1262,99 +1265,99 @@ |
| 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 |
|
| ... | ... |
@@ -1985,71 +1988,70 @@ |
| 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; |
| ... | ... |
@@ -331,79 +331,79 @@ |
| 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. |
| ... | ... |
@@ -431,98 +431,98 @@ |
| 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 |
|
| ... | ... |
@@ -887,87 +887,88 @@ |
| 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: |
| ... | ... |
@@ -987,98 +988,98 @@ |
| 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 |
|
| ... | ... |
@@ -1489,70 +1490,71 @@ |
| 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; |
| ... | ... |
@@ -123,49 +123,49 @@ |
| 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 |
| ... | ... |
@@ -125,49 +125,49 @@ |
| 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 |
} |
| ... | ... |
@@ -121,49 +121,49 @@ |
| 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]); |
| ... | ... |
@@ -146,42 +146,42 @@ |
| 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)