0
5
0
2
2
96
63
64
42
... | ... |
@@ -25,41 +25,39 @@ |
25 | 25 |
/// "DigraphReader" class. |
26 | 26 |
/// |
27 | 27 |
/// The \c "digraph.lgf" file: |
28 | 28 |
/// \include digraph.lgf |
29 | 29 |
/// |
30 | 30 |
/// And the program which reads it: |
31 | 31 |
/// \include lgf_demo.cc |
32 | 32 |
|
33 | 33 |
#include <iostream> |
34 | 34 |
#include <lemon/smart_graph.h> |
35 | 35 |
#include <lemon/lgf_reader.h> |
36 | 36 |
#include <lemon/lgf_writer.h> |
37 |
#include <lemon/random.h> |
|
38 |
|
|
39 | 37 |
|
40 | 38 |
using namespace lemon; |
41 | 39 |
|
42 | 40 |
int main() { |
43 | 41 |
SmartDigraph g; |
44 | 42 |
SmartDigraph::ArcMap<int> cap(g); |
45 | 43 |
SmartDigraph::Node s, t; |
46 | 44 |
|
47 |
digraphReader("digraph.lgf", g). // read the |
|
45 |
digraphReader("digraph.lgf", g). // read the directed graph into g |
|
48 | 46 |
arcMap("capacity", cap). // read the 'capacity' arc map into cap |
49 | 47 |
node("source", s). // read 'source' node to s |
50 | 48 |
node("target", t). // read 'target' node to t |
51 | 49 |
run(); |
52 | 50 |
|
53 |
std::cout << " |
|
51 |
std::cout << "A digraph is read from 'digraph.lgf'." << std::endl; |
|
54 | 52 |
std::cout << "Number of nodes: " << countNodes(g) << std::endl; |
55 | 53 |
std::cout << "Number of arcs: " << countArcs(g) << std::endl; |
56 | 54 |
|
57 | 55 |
std::cout << "We can write it to the standard output:" << std::endl; |
58 | 56 |
|
59 | 57 |
digraphWriter(std::cout, g). // write g to the standard output |
60 | 58 |
arcMap("capacity", cap). // write cap into 'capacity' |
61 | 59 |
node("source", s). // write s to 'source' |
62 | 60 |
node("target", t). // write t to 'target' |
63 | 61 |
run(); |
64 | 62 |
|
65 | 63 |
return 0; |
... | ... |
@@ -466,29 +466,27 @@ |
466 | 466 |
/** |
467 | 467 |
@defgroup io_group Input-Output |
468 | 468 |
\brief Graph Input-Output methods |
469 | 469 |
|
470 | 470 |
This group describes the tools for importing and exporting graphs |
471 | 471 |
and graph related data. Now it supports the LEMON format, the |
472 | 472 |
\c DIMACS format and the encapsulated postscript (EPS) format. |
473 | 473 |
*/ |
474 | 474 |
|
475 | 475 |
/** |
476 | 476 |
@defgroup lemon_io Lemon Input-Output |
477 | 477 |
@ingroup io_group |
478 |
\brief Reading and writing |
|
478 |
\brief Reading and writing \ref lgf-format "Lemon Graph Format". |
|
479 | 479 |
|
480 |
This group describes methods for reading and writing LEMON format. |
|
481 |
You can find more about this format on the \ref graph-io-page "Graph Input-Output" |
|
482 |
|
|
480 |
This group describes methods for reading and writing \ref lgf-format "Lemon Graph Format". |
|
483 | 481 |
*/ |
484 | 482 |
|
485 | 483 |
/** |
486 | 484 |
@defgroup eps_io Postscript exporting |
487 | 485 |
@ingroup io_group |
488 | 486 |
\brief General \c EPS drawer and graph exporter |
489 | 487 |
|
490 | 488 |
This group describes general \c EPS drawing methods and special |
491 | 489 |
graph exporting tools. |
492 | 490 |
*/ |
493 | 491 |
|
494 | 492 |
... | ... |
@@ -37,43 +37,43 @@ |
37 | 37 |
and \@attributes. Each header line may also have an optional |
38 | 38 |
\e name, which can be use to distinguish the sections of the same |
39 | 39 |
type. |
40 | 40 |
|
41 | 41 |
The standard sections are column oriented, each line consists of |
42 | 42 |
<em>token</em>s separated by whitespaces. A token can be \e plain or |
43 | 43 |
\e quoted. A plain token is just a sequence of non-whitespace characters, |
44 | 44 |
while a quoted token is a |
45 | 45 |
character sequence surrounded by double quotes, and it can also |
46 | 46 |
contain whitespaces and escape sequences. |
47 | 47 |
|
48 | 48 |
The \c \@nodes section describes a set of nodes and associated |
49 |
maps. The first is a header line, |
|
49 |
maps. The first is a header line, its columns are the names of the |
|
50 | 50 |
maps appearing in the following lines. |
51 | 51 |
One of the maps must be called \c |
52 | 52 |
"label", which plays special role in the file. |
53 | 53 |
The following |
54 | 54 |
non-empty lines until the next section describes nodes of the |
55 | 55 |
graph. Each line contains the values of the node maps |
56 | 56 |
associated to the current node. |
57 | 57 |
|
58 | 58 |
\code |
59 | 59 |
@nodes |
60 | 60 |
label coordinates size title |
61 | 61 |
1 (10,20) 10 "First node" |
62 | 62 |
2 (80,80) 8 "Second node" |
63 | 63 |
3 (40,10) 10 "Third node" |
64 | 64 |
\endcode |
65 | 65 |
|
66 | 66 |
The \c \@arcs section is very similar to the \c \@nodes section, |
67 |
it again starts with a header line describing the names of the |
|
67 |
it again starts with a header line describing the names of the maps, |
|
68 | 68 |
but the \c "label" map is not obligatory here. The following lines |
69 | 69 |
describe the arcs. The first two tokens of each line are |
70 | 70 |
the source and the target node of the arc, respectively, then come the map |
71 | 71 |
values. The source and target tokens must be node labels. |
72 | 72 |
|
73 | 73 |
\code |
74 | 74 |
@arcs |
75 | 75 |
capacity |
76 | 76 |
1 2 16 |
77 | 77 |
1 3 12 |
78 | 78 |
2 3 18 |
79 | 79 |
\endcode |
... | ... |
@@ -9,25 +9,25 @@ |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 |
///\brief Lemon Graph Format reader. |
|
21 |
///\brief \ref lgf-format "Lemon Graph Format" reader. |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_READER_H |
25 | 25 |
#define LEMON_LGF_READER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <set> |
32 | 32 |
#include <map> |
33 | 33 |
|
... | ... |
@@ -391,69 +391,69 @@ |
391 | 391 |
|
392 | 392 |
template <typename Digraph> |
393 | 393 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph); |
394 | 394 |
|
395 | 395 |
template <typename Digraph> |
396 | 396 |
DigraphReader<Digraph> digraphReader(const std::string& fn, Digraph& digraph); |
397 | 397 |
|
398 | 398 |
template <typename Digraph> |
399 | 399 |
DigraphReader<Digraph> digraphReader(const char *fn, Digraph& digraph); |
400 | 400 |
|
401 | 401 |
/// \ingroup lemon_io |
402 | 402 |
/// |
403 |
/// \brief LGF reader for directed graphs |
|
403 |
/// \brief \ref lgf-format "LGF" reader for directed graphs |
|
404 | 404 |
/// |
405 | 405 |
/// This utility reads an \ref lgf-format "LGF" file. |
406 | 406 |
/// |
407 | 407 |
/// The reading method does a batch processing. The user creates a |
408 | 408 |
/// reader object, then various reading rules can be added to the |
409 | 409 |
/// reader, and eventually the reading is executed with the \c run() |
410 | 410 |
/// member function. A map reading rule can be added to the reader |
411 | 411 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
412 | 412 |
/// converter parameter can also be added as a standard functor |
413 |
/// converting from std::string to the value type of the map. If it |
|
413 |
/// converting from \c std::string to the value type of the map. If it |
|
414 | 414 |
/// is set, it will determine how the tokens in the file should be |
415 |
/// |
|
415 |
/// converted to the value type of the map. If the functor is not set, |
|
416 | 416 |
/// then a default conversion will be used. One map can be read into |
417 | 417 |
/// multiple map objects at the same time. The \c attribute(), \c |
418 | 418 |
/// node() and \c arc() functions are used to add attribute reading |
419 | 419 |
/// rules. |
420 | 420 |
/// |
421 | 421 |
///\code |
422 |
/// DigraphReader<Digraph>(std::cin, digraph). |
|
423 |
/// nodeMap("coordinates", coord_map). |
|
424 |
/// arcMap("capacity", cap_map). |
|
425 |
/// node("source", src). |
|
426 |
/// node("target", trg). |
|
427 |
/// attribute("caption", caption). |
|
428 |
/// |
|
422 |
/// DigraphReader<Digraph>(std::cin, digraph). |
|
423 |
/// nodeMap("coordinates", coord_map). |
|
424 |
/// arcMap("capacity", cap_map). |
|
425 |
/// node("source", src). |
|
426 |
/// node("target", trg). |
|
427 |
/// attribute("caption", caption). |
|
428 |
/// run(); |
|
429 | 429 |
///\endcode |
430 | 430 |
/// |
431 | 431 |
/// By default the reader uses the first section in the file of the |
432 | 432 |
/// proper type. If a section has an optional name, then it can be |
433 | 433 |
/// selected for reading by giving an optional name parameter to the |
434 | 434 |
/// \c nodes(), \c arcs() or \c attributes() functions. |
435 | 435 |
/// |
436 | 436 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader |
437 | 437 |
/// that the nodes or arcs should not be constructed (added to the |
438 | 438 |
/// graph) during the reading, but instead the label map of the items |
439 | 439 |
/// are given as a parameter of these functions. An |
440 |
/// application of these function is multipass reading, which is |
|
441 |
/// important if two \e \@arcs sections must be read from the |
|
442 |
/// |
|
440 |
/// application of these functions is multipass reading, which is |
|
441 |
/// important if two \c \@arcs sections must be read from the |
|
442 |
/// file. In this case the first phase would read the node set and one |
|
443 | 443 |
/// of the arc sets, while the second phase would read the second arc |
444 | 444 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet). |
445 | 445 |
/// The previously read label node map should be passed to the \c |
446 | 446 |
/// useNodes() functions. Another application of multipass reading when |
447 |
/// paths are given as a node map or an arc map. It is impossible read this in |
|
447 |
/// paths are given as a node map or an arc map. It is impossible to read this in |
|
448 | 448 |
/// a single pass, because the arcs are not constructed when the node |
449 | 449 |
/// maps are read. |
450 | 450 |
template <typename _Digraph> |
451 | 451 |
class DigraphReader { |
452 | 452 |
public: |
453 | 453 |
|
454 | 454 |
typedef _Digraph Digraph; |
455 | 455 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); |
456 | 456 |
|
457 | 457 |
private: |
458 | 458 |
|
459 | 459 |
|
... | ... |
@@ -726,25 +726,25 @@ |
726 | 726 |
_use_nodes = true; |
727 | 727 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
728 | 728 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
729 | 729 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
730 | 730 |
} |
731 | 731 |
return *this; |
732 | 732 |
} |
733 | 733 |
|
734 | 734 |
/// \brief Use previously constructed node set |
735 | 735 |
/// |
736 | 736 |
/// Use previously constructed node set, and specify the node |
737 | 737 |
/// label map and a functor which converts the label map values to |
738 |
/// std::string. |
|
738 |
/// \c std::string. |
|
739 | 739 |
template <typename Map, typename Converter> |
740 | 740 |
DigraphReader& useNodes(const Map& map, |
741 | 741 |
const Converter& converter = Converter()) { |
742 | 742 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
743 | 743 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
744 | 744 |
_use_nodes = true; |
745 | 745 |
for (NodeIt n(_digraph); n != INVALID; ++n) { |
746 | 746 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
747 | 747 |
} |
748 | 748 |
return *this; |
749 | 749 |
} |
750 | 750 |
|
... | ... |
@@ -759,56 +759,55 @@ |
759 | 759 |
_use_arcs = true; |
760 | 760 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
761 | 761 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
762 | 762 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
763 | 763 |
} |
764 | 764 |
return *this; |
765 | 765 |
} |
766 | 766 |
|
767 | 767 |
/// \brief Use previously constructed arc set |
768 | 768 |
/// |
769 | 769 |
/// Use previously constructed arc set, and specify the arc |
770 | 770 |
/// label map and a functor which converts the label map values to |
771 |
/// std::string. |
|
771 |
/// \c std::string. |
|
772 | 772 |
template <typename Map, typename Converter> |
773 | 773 |
DigraphReader& useArcs(const Map& map, |
774 | 774 |
const Converter& converter = Converter()) { |
775 | 775 |
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>(); |
776 | 776 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member"); |
777 | 777 |
_use_arcs = true; |
778 | 778 |
for (ArcIt a(_digraph); a != INVALID; ++a) { |
779 | 779 |
_arc_index.insert(std::make_pair(converter(map[a]), a)); |
780 | 780 |
} |
781 | 781 |
return *this; |
782 | 782 |
} |
783 | 783 |
|
784 | 784 |
/// \brief Skips the reading of node section |
785 | 785 |
/// |
786 | 786 |
/// Omit the reading of the node section. This implies that each node |
787 |
/// map reading rule will be |
|
787 |
/// map reading rule will be abandoned, and the nodes of the graph |
|
788 | 788 |
/// will not be constructed, which usually cause that the arc set |
789 |
/// could not be read due to lack of node name |
|
790 |
/// resolving. Therefore, the \c skipArcs() should be used too, or |
|
791 |
/// the useNodes() member function should be used to specify the |
|
792 |
/// label of the nodes. |
|
789 |
/// could not be read due to lack of node name resolving. |
|
790 |
/// Therefore \c skipArcs() function should also be used, or |
|
791 |
/// \c useNodes() should be used to specify the label of the nodes. |
|
793 | 792 |
DigraphReader& skipNodes() { |
794 | 793 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
795 | 794 |
_skip_nodes = true; |
796 | 795 |
return *this; |
797 | 796 |
} |
798 | 797 |
|
799 | 798 |
/// \brief Skips the reading of arc section |
800 | 799 |
/// |
801 | 800 |
/// Omit the reading of the arc section. This implies that each arc |
802 |
/// map reading rule will be |
|
801 |
/// map reading rule will be abandoned, and the arcs of the graph |
|
803 | 802 |
/// will not be constructed. |
804 | 803 |
DigraphReader& skipArcs() { |
805 | 804 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set"); |
806 | 805 |
_skip_arcs = true; |
807 | 806 |
return *this; |
808 | 807 |
} |
809 | 808 |
|
810 | 809 |
/// @} |
811 | 810 |
|
812 | 811 |
private: |
813 | 812 |
|
814 | 813 |
bool readLine() { |
... | ... |
@@ -1166,63 +1165,76 @@ |
1166 | 1165 |
} |
1167 | 1166 |
|
1168 | 1167 |
if (!attributes_done && !_attributes.empty()) { |
1169 | 1168 |
throw DataFormatError("Section @attributes not found"); |
1170 | 1169 |
} |
1171 | 1170 |
|
1172 | 1171 |
} |
1173 | 1172 |
|
1174 | 1173 |
/// @} |
1175 | 1174 |
|
1176 | 1175 |
}; |
1177 | 1176 |
|
1177 |
/// \brief Return a \ref DigraphReader class |
|
1178 |
/// |
|
1179 |
/// This function just returns a \ref DigraphReader class. |
|
1178 | 1180 |
/// \relates DigraphReader |
1179 | 1181 |
template <typename Digraph> |
1180 | 1182 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) { |
1181 | 1183 |
DigraphReader<Digraph> tmp(is, digraph); |
1182 | 1184 |
return tmp; |
1183 | 1185 |
} |
1184 | 1186 |
|
1187 |
/// \brief Return a \ref DigraphReader class |
|
1188 |
/// |
|
1189 |
/// This function just returns a \ref DigraphReader class. |
|
1185 | 1190 |
/// \relates DigraphReader |
1186 | 1191 |
template <typename Digraph> |
1187 | 1192 |
DigraphReader<Digraph> digraphReader(const std::string& fn, |
1188 | 1193 |
Digraph& digraph) { |
1189 | 1194 |
DigraphReader<Digraph> tmp(fn, digraph); |
1190 | 1195 |
return tmp; |
1191 | 1196 |
} |
1192 | 1197 |
|
1198 |
/// \brief Return a \ref DigraphReader class |
|
1199 |
/// |
|
1200 |
/// This function just returns a \ref DigraphReader class. |
|
1193 | 1201 |
/// \relates DigraphReader |
1194 | 1202 |
template <typename Digraph> |
1195 | 1203 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) { |
1196 | 1204 |
DigraphReader<Digraph> tmp(fn, digraph); |
1197 | 1205 |
return tmp; |
1198 | 1206 |
} |
1199 | 1207 |
|
1200 | 1208 |
template <typename Graph> |
1201 | 1209 |
class GraphReader; |
1202 | 1210 |
|
1203 | 1211 |
template <typename Graph> |
1204 | 1212 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph); |
1205 | 1213 |
|
1206 | 1214 |
template <typename Graph> |
1207 | 1215 |
GraphReader<Graph> graphReader(const std::string& fn, Graph& graph); |
1208 | 1216 |
|
1209 | 1217 |
template <typename Graph> |
1210 | 1218 |
GraphReader<Graph> graphReader(const char *fn, Graph& graph); |
1211 | 1219 |
|
1212 | 1220 |
/// \ingroup lemon_io |
1213 | 1221 |
/// |
1214 |
/// \brief LGF reader for undirected graphs |
|
1222 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs |
|
1215 | 1223 |
/// |
1216 | 1224 |
/// This utility reads an \ref lgf-format "LGF" file. |
1225 |
/// |
|
1226 |
/// It can be used almost the same way as \c DigraphReader. |
|
1227 |
/// The only difference is that this class can handle edges and |
|
1228 |
/// edge maps as well as arcs and arc maps. |
|
1217 | 1229 |
template <typename _Graph> |
1218 | 1230 |
class GraphReader { |
1219 | 1231 |
public: |
1220 | 1232 |
|
1221 | 1233 |
typedef _Graph Graph; |
1222 | 1234 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
1223 | 1235 |
|
1224 | 1236 |
private: |
1225 | 1237 |
|
1226 | 1238 |
std::istream* _is; |
1227 | 1239 |
bool local_is; |
1228 | 1240 |
|
... | ... |
@@ -1253,43 +1265,43 @@ |
1253 | 1265 |
bool _use_edges; |
1254 | 1266 |
|
1255 | 1267 |
bool _skip_nodes; |
1256 | 1268 |
bool _skip_edges; |
1257 | 1269 |
|
1258 | 1270 |
int line_num; |
1259 | 1271 |
std::istringstream line; |
1260 | 1272 |
|
1261 | 1273 |
public: |
1262 | 1274 |
|
1263 | 1275 |
/// \brief Constructor |
1264 | 1276 |
/// |
1265 |
/// Construct |
|
1277 |
/// Construct an undirected graph reader, which reads from the given |
|
1266 | 1278 |
/// input stream. |
1267 | 1279 |
GraphReader(std::istream& is, Graph& graph) |
1268 | 1280 |
: _is(&is), local_is(false), _graph(graph), |
1269 | 1281 |
_use_nodes(false), _use_edges(false), |
1270 | 1282 |
_skip_nodes(false), _skip_edges(false) {} |
1271 | 1283 |
|
1272 | 1284 |
/// \brief Constructor |
1273 | 1285 |
/// |
1274 |
/// Construct |
|
1286 |
/// Construct an undirected graph reader, which reads from the given |
|
1275 | 1287 |
/// file. |
1276 | 1288 |
GraphReader(const std::string& fn, Graph& graph) |
1277 | 1289 |
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph), |
1278 | 1290 |
_use_nodes(false), _use_edges(false), |
1279 | 1291 |
_skip_nodes(false), _skip_edges(false) {} |
1280 | 1292 |
|
1281 | 1293 |
/// \brief Constructor |
1282 | 1294 |
/// |
1283 |
/// Construct |
|
1295 |
/// Construct an undirected graph reader, which reads from the given |
|
1284 | 1296 |
/// file. |
1285 | 1297 |
GraphReader(const char* fn, Graph& graph) |
1286 | 1298 |
: _is(new std::ifstream(fn)), local_is(true), _graph(graph), |
1287 | 1299 |
_use_nodes(false), _use_edges(false), |
1288 | 1300 |
_skip_nodes(false), _skip_edges(false) {} |
1289 | 1301 |
|
1290 | 1302 |
/// \brief Destructor |
1291 | 1303 |
~GraphReader() { |
1292 | 1304 |
for (typename NodeMaps::iterator it = _node_maps.begin(); |
1293 | 1305 |
it != _node_maps.end(); ++it) { |
1294 | 1306 |
delete it->second; |
1295 | 1307 |
} |
... | ... |
@@ -1488,41 +1500,41 @@ |
1488 | 1500 |
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter); |
1489 | 1501 |
_attributes.insert(std::make_pair(caption, storage)); |
1490 | 1502 |
return *this; |
1491 | 1503 |
} |
1492 | 1504 |
|
1493 | 1505 |
/// @} |
1494 | 1506 |
|
1495 | 1507 |
/// \name Select section by name |
1496 | 1508 |
/// @{ |
1497 | 1509 |
|
1498 | 1510 |
/// \brief Set \c \@nodes section to be read |
1499 | 1511 |
/// |
1500 |
/// Set \c \@nodes section to be read |
|
1512 |
/// Set \c \@nodes section to be read. |
|
1501 | 1513 |
GraphReader& nodes(const std::string& caption) { |
1502 | 1514 |
_nodes_caption = caption; |
1503 | 1515 |
return *this; |
1504 | 1516 |
} |
1505 | 1517 |
|
1506 | 1518 |
/// \brief Set \c \@edges section to be read |
1507 | 1519 |
/// |
1508 |
/// Set \c \@edges section to be read |
|
1520 |
/// Set \c \@edges section to be read. |
|
1509 | 1521 |
GraphReader& edges(const std::string& caption) { |
1510 | 1522 |
_edges_caption = caption; |
1511 | 1523 |
return *this; |
1512 | 1524 |
} |
1513 | 1525 |
|
1514 | 1526 |
/// \brief Set \c \@attributes section to be read |
1515 | 1527 |
/// |
1516 |
/// Set \c \@attributes section to be read |
|
1528 |
/// Set \c \@attributes section to be read. |
|
1517 | 1529 |
GraphReader& attributes(const std::string& caption) { |
1518 | 1530 |
_attributes_caption = caption; |
1519 | 1531 |
return *this; |
1520 | 1532 |
} |
1521 | 1533 |
|
1522 | 1534 |
/// @} |
1523 | 1535 |
|
1524 | 1536 |
/// \name Using previously constructed node or edge set |
1525 | 1537 |
/// @{ |
1526 | 1538 |
|
1527 | 1539 |
/// \brief Use previously constructed node set |
1528 | 1540 |
/// |
... | ... |
@@ -1535,25 +1547,25 @@ |
1535 | 1547 |
_use_nodes = true; |
1536 | 1548 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
1537 | 1549 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1538 | 1550 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
1539 | 1551 |
} |
1540 | 1552 |
return *this; |
1541 | 1553 |
} |
1542 | 1554 |
|
1543 | 1555 |
/// \brief Use previously constructed node set |
1544 | 1556 |
/// |
1545 | 1557 |
/// Use previously constructed node set, and specify the node |
1546 | 1558 |
/// label map and a functor which converts the label map values to |
1547 |
/// std::string. |
|
1559 |
/// \c std::string. |
|
1548 | 1560 |
template <typename Map, typename Converter> |
1549 | 1561 |
GraphReader& useNodes(const Map& map, |
1550 | 1562 |
const Converter& converter = Converter()) { |
1551 | 1563 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1552 | 1564 |
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member"); |
1553 | 1565 |
_use_nodes = true; |
1554 | 1566 |
for (NodeIt n(_graph); n != INVALID; ++n) { |
1555 | 1567 |
_node_index.insert(std::make_pair(converter(map[n]), n)); |
1556 | 1568 |
} |
1557 | 1569 |
return *this; |
1558 | 1570 |
} |
1559 | 1571 |
|
... | ... |
@@ -1568,56 +1580,56 @@ |
1568 | 1580 |
_use_edges = true; |
1569 | 1581 |
_writer_bits::DefaultConverter<typename Map::Value> converter; |
1570 | 1582 |
for (EdgeIt a(_graph); a != INVALID; ++a) { |
1571 | 1583 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
1572 | 1584 |
} |
1573 | 1585 |
return *this; |
1574 | 1586 |
} |
1575 | 1587 |
|
1576 | 1588 |
/// \brief Use previously constructed edge set |
1577 | 1589 |
/// |
1578 | 1590 |
/// Use previously constructed edge set, and specify the edge |
1579 | 1591 |
/// label map and a functor which converts the label map values to |
1580 |
/// std::string. |
|
1592 |
/// \c std::string. |
|
1581 | 1593 |
template <typename Map, typename Converter> |
1582 | 1594 |
GraphReader& useEdges(const Map& map, |
1583 | 1595 |
const Converter& converter = Converter()) { |
1584 | 1596 |
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>(); |
1585 | 1597 |
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member"); |
1586 | 1598 |
_use_edges = true; |
1587 | 1599 |
for (EdgeIt a(_graph); a != INVALID; ++a) { |
1588 | 1600 |
_edge_index.insert(std::make_pair(converter(map[a]), a)); |
1589 | 1601 |
} |
1590 | 1602 |
return *this; |
1591 | 1603 |
} |
1592 | 1604 |
|
1593 |
/// \brief |
|
1605 |
/// \brief Skip the reading of node section |
|
1594 | 1606 |
/// |
1595 | 1607 |
/// Omit the reading of the node section. This implies that each node |
1596 |
/// map reading rule will be |
|
1608 |
/// map reading rule will be abandoned, and the nodes of the graph |
|
1597 | 1609 |
/// will not be constructed, which usually cause that the edge set |
1598 | 1610 |
/// could not be read due to lack of node name |
1599 |
/// resolving. Therefore, the \c skipEdges() should be used too, or |
|
1600 |
/// the useNodes() member function should be used to specify the |
|
1601 |
/// |
|
1611 |
/// could not be read due to lack of node name resolving. |
|
1612 |
/// Therefore \c skipEdges() function should also be used, or |
|
1613 |
/// \c useNodes() should be used to specify the label of the nodes. |
|
1602 | 1614 |
GraphReader& skipNodes() { |
1603 | 1615 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set"); |
1604 | 1616 |
_skip_nodes = true; |
1605 | 1617 |
return *this; |
1606 | 1618 |
} |
1607 | 1619 |
|
1608 |
/// \brief |
|
1620 |
/// \brief Skip the reading of edge section |
|
1609 | 1621 |
/// |
1610 | 1622 |
/// Omit the reading of the edge section. This implies that each edge |
1611 |
/// map reading rule will be |
|
1623 |
/// map reading rule will be abandoned, and the edges of the graph |
|
1612 | 1624 |
/// will not be constructed. |
1613 | 1625 |
GraphReader& skipEdges() { |
1614 | 1626 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set"); |
1615 | 1627 |
_skip_edges = true; |
1616 | 1628 |
return *this; |
1617 | 1629 |
} |
1618 | 1630 |
|
1619 | 1631 |
/// @} |
1620 | 1632 |
|
1621 | 1633 |
private: |
1622 | 1634 |
|
1623 | 1635 |
bool readLine() { |
... | ... |
@@ -1973,59 +1985,70 @@ |
1973 | 1985 |
} |
1974 | 1986 |
|
1975 | 1987 |
if (!attributes_done && !_attributes.empty()) { |
1976 | 1988 |
throw DataFormatError("Section @attributes not found"); |
1977 | 1989 |
} |
1978 | 1990 |
|
1979 | 1991 |
} |
1980 | 1992 |
|
1981 | 1993 |
/// @} |
1982 | 1994 |
|
1983 | 1995 |
}; |
1984 | 1996 |
|
1997 |
/// \brief Return a \ref GraphReader class |
|
1998 |
/// |
|
1999 |
/// This function just returns a \ref GraphReader class. |
|
1985 | 2000 |
/// \relates GraphReader |
1986 | 2001 |
template <typename Graph> |
1987 | 2002 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph) { |
1988 | 2003 |
GraphReader<Graph> tmp(is, graph); |
1989 | 2004 |
return tmp; |
1990 | 2005 |
} |
1991 | 2006 |
|
2007 |
/// \brief Return a \ref GraphReader class |
|
2008 |
/// |
|
2009 |
/// This function just returns a \ref GraphReader class. |
|
1992 | 2010 |
/// \relates GraphReader |
1993 | 2011 |
template <typename Graph> |
1994 | 2012 |
GraphReader<Graph> graphReader(const std::string& fn, |
1995 | 2013 |
Graph& graph) { |
1996 | 2014 |
GraphReader<Graph> tmp(fn, graph); |
1997 | 2015 |
return tmp; |
1998 | 2016 |
} |
1999 | 2017 |
|
2018 |
/// \brief Return a \ref GraphReader class |
|
2019 |
/// |
|
2020 |
/// This function just returns a \ref GraphReader class. |
|
2000 | 2021 |
/// \relates GraphReader |
2001 | 2022 |
template <typename Graph> |
2002 | 2023 |
GraphReader<Graph> graphReader(const char* fn, Graph& graph) { |
2003 | 2024 |
GraphReader<Graph> tmp(fn, graph); |
2004 | 2025 |
return tmp; |
2005 | 2026 |
} |
2006 | 2027 |
|
2007 | 2028 |
class SectionReader; |
2008 | 2029 |
|
2009 | 2030 |
SectionReader sectionReader(std::istream& is); |
2010 | 2031 |
SectionReader sectionReader(const std::string& fn); |
2011 | 2032 |
SectionReader sectionReader(const char* fn); |
2012 | 2033 |
|
2034 |
/// \ingroup lemon_io |
|
2035 |
/// |
|
2013 | 2036 |
/// \brief Section reader class |
2014 | 2037 |
/// |
2015 |
/// In the \e LGF file extra sections can be placed, which contain |
|
2016 |
/// any data in arbitrary format. Such sections can be read with |
|
2017 |
/// this class. A reading rule can be added with two different |
|
2018 |
/// functions, with the \c sectionLines() function a functor can |
|
2019 |
/// |
|
2038 |
/// In the \ref lgf-format "LGF" file extra sections can be placed, |
|
2039 |
/// which contain any data in arbitrary format. Such sections can be |
|
2040 |
/// read with this class. A reading rule can be added to the class |
|
2041 |
/// with two different functions. With the \c sectionLines() function a |
|
2042 |
/// functor can process the section line-by-line, while with the \c |
|
2020 | 2043 |
/// sectionStream() member the section can be read from an input |
2021 | 2044 |
/// stream. |
2022 | 2045 |
class SectionReader { |
2023 | 2046 |
private: |
2024 | 2047 |
|
2025 | 2048 |
std::istream* _is; |
2026 | 2049 |
bool local_is; |
2027 | 2050 |
|
2028 | 2051 |
typedef std::map<std::string, _reader_bits::Section*> Sections; |
2029 | 2052 |
Sections _sections; |
2030 | 2053 |
|
2031 | 2054 |
int line_num; |
... | ... |
@@ -2096,61 +2119,61 @@ |
2096 | 2119 |
/// and the comment lines are filtered out, and the leading |
2097 | 2120 |
/// whitespaces are trimmed from each processed string. |
2098 | 2121 |
/// |
2099 | 2122 |
/// For example let's see a section, which contain several |
2100 | 2123 |
/// integers, which should be inserted into a vector. |
2101 | 2124 |
///\code |
2102 | 2125 |
/// @numbers |
2103 | 2126 |
/// 12 45 23 |
2104 | 2127 |
/// 4 |
2105 | 2128 |
/// 23 6 |
2106 | 2129 |
///\endcode |
2107 | 2130 |
/// |
2108 |
/// The functor is implemented as |
|
2131 |
/// The functor is implemented as a struct: |
|
2109 | 2132 |
///\code |
2110 | 2133 |
/// struct NumberSection { |
2111 | 2134 |
/// std::vector<int>& _data; |
2112 | 2135 |
/// NumberSection(std::vector<int>& data) : _data(data) {} |
2113 | 2136 |
/// void operator()(const std::string& line) { |
2114 | 2137 |
/// std::istringstream ls(line); |
2115 | 2138 |
/// int value; |
2116 | 2139 |
/// while (ls >> value) _data.push_back(value); |
2117 | 2140 |
/// } |
2118 | 2141 |
/// }; |
2119 | 2142 |
/// |
2120 | 2143 |
/// // ... |
2121 | 2144 |
/// |
2122 | 2145 |
/// reader.sectionLines("numbers", NumberSection(vec)); |
2123 | 2146 |
///\endcode |
2124 | 2147 |
template <typename Functor> |
2125 | 2148 |
SectionReader& sectionLines(const std::string& type, Functor functor) { |
2126 |
LEMON_ASSERT(!type.empty(), "Type is |
|
2149 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
|
2127 | 2150 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2128 | 2151 |
"Multiple reading of section."); |
2129 | 2152 |
_sections.insert(std::make_pair(type, |
2130 | 2153 |
new _reader_bits::LineSection<Functor>(functor))); |
2131 | 2154 |
return *this; |
2132 | 2155 |
} |
2133 | 2156 |
|
2134 | 2157 |
|
2135 | 2158 |
/// \brief Add a section processor with stream oriented reading |
2136 | 2159 |
/// |
2137 | 2160 |
/// The first parameter is the type of the section, the second is |
2138 |
/// a functor, which takes an \c std::istream& and an int& |
|
2161 |
/// a functor, which takes an \c std::istream& and an \c int& |
|
2139 | 2162 |
/// parameter, the latter regard to the line number of stream. The |
2140 | 2163 |
/// functor can read the input while the section go on, and the |
2141 | 2164 |
/// line number should be modified accordingly. |
2142 | 2165 |
template <typename Functor> |
2143 | 2166 |
SectionReader& sectionStream(const std::string& type, Functor functor) { |
2144 |
LEMON_ASSERT(!type.empty(), "Type is |
|
2167 |
LEMON_ASSERT(!type.empty(), "Type is empty."); |
|
2145 | 2168 |
LEMON_ASSERT(_sections.find(type) == _sections.end(), |
2146 | 2169 |
"Multiple reading of section."); |
2147 | 2170 |
_sections.insert(std::make_pair(type, |
2148 | 2171 |
new _reader_bits::StreamSection<Functor>(functor))); |
2149 | 2172 |
return *this; |
2150 | 2173 |
} |
2151 | 2174 |
|
2152 | 2175 |
/// @} |
2153 | 2176 |
|
2154 | 2177 |
private: |
2155 | 2178 |
|
2156 | 2179 |
bool readLine() { |
... | ... |
@@ -2177,25 +2200,25 @@ |
2177 | 2200 |
} |
2178 | 2201 |
line.putback(c); |
2179 | 2202 |
} |
2180 | 2203 |
|
2181 | 2204 |
public: |
2182 | 2205 |
|
2183 | 2206 |
|
2184 | 2207 |
/// \name Execution of the reader |
2185 | 2208 |
/// @{ |
2186 | 2209 |
|
2187 | 2210 |
/// \brief Start the batch processing |
2188 | 2211 |
/// |
2189 |
/// This function starts the batch processing |
|
2212 |
/// This function starts the batch processing. |
|
2190 | 2213 |
void run() { |
2191 | 2214 |
|
2192 | 2215 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader"); |
2193 | 2216 |
|
2194 | 2217 |
std::set<std::string> extra_sections; |
2195 | 2218 |
|
2196 | 2219 |
line_num = 0; |
2197 | 2220 |
readLine(); |
2198 | 2221 |
skipSection(); |
2199 | 2222 |
|
2200 | 2223 |
while (readSuccess()) { |
2201 | 2224 |
try { |
... | ... |
@@ -2230,67 +2253,77 @@ |
2230 | 2253 |
if (extra_sections.find(it->first) == extra_sections.end()) { |
2231 | 2254 |
std::ostringstream os; |
2232 | 2255 |
os << "Cannot find section: " << it->first; |
2233 | 2256 |
throw DataFormatError(os.str().c_str()); |
2234 | 2257 |
} |
2235 | 2258 |
} |
2236 | 2259 |
} |
2237 | 2260 |
|
2238 | 2261 |
/// @} |
2239 | 2262 |
|
2240 | 2263 |
}; |
2241 | 2264 |
|
2265 |
/// \brief Return a \ref SectionReader class |
|
2266 |
/// |
|
2267 |
/// This function just returns a \ref SectionReader class. |
|
2242 | 2268 |
/// \relates SectionReader |
2243 | 2269 |
inline SectionReader sectionReader(std::istream& is) { |
2244 | 2270 |
SectionReader tmp(is); |
2245 | 2271 |
return tmp; |
2246 | 2272 |
} |
2247 | 2273 |
|
2274 |
/// \brief Return a \ref SectionReader class |
|
2275 |
/// |
|
2276 |
/// This function just returns a \ref SectionReader class. |
|
2248 | 2277 |
/// \relates SectionReader |
2249 | 2278 |
inline SectionReader sectionReader(const std::string& fn) { |
2250 | 2279 |
SectionReader tmp(fn); |
2251 | 2280 |
return tmp; |
2252 | 2281 |
} |
2253 | 2282 |
|
2283 |
/// \brief Return a \ref SectionReader class |
|
2284 |
/// |
|
2285 |
/// This function just returns a \ref SectionReader class. |
|
2254 | 2286 |
/// \relates SectionReader |
2255 | 2287 |
inline SectionReader sectionReader(const char* fn) { |
2256 | 2288 |
SectionReader tmp(fn); |
2257 | 2289 |
return tmp; |
2258 | 2290 |
} |
2259 | 2291 |
|
2260 | 2292 |
/// \ingroup lemon_io |
2261 | 2293 |
/// |
2262 | 2294 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file |
2263 | 2295 |
/// |
2264 | 2296 |
/// This class can be used to read the sections, the map names and |
2265 | 2297 |
/// the attributes from a file. Usually, the Lemon programs know |
2266 | 2298 |
/// that, which type of graph, which maps and which attributes |
2267 | 2299 |
/// should be read from a file, but in general tools (like glemon) |
2268 | 2300 |
/// the contents of an LGF file should be guessed somehow. This class |
2269 | 2301 |
/// reads the graph and stores the appropriate information for |
2270 | 2302 |
/// reading the graph. |
2271 | 2303 |
/// |
2272 |
///\code |
|
2304 |
///\code |
|
2305 |
/// LgfContents contents("graph.lgf"); |
|
2273 | 2306 |
/// contents.run(); |
2274 | 2307 |
/// |
2275 |
/// // |
|
2308 |
/// // Does it contain any node section and arc section? |
|
2276 | 2309 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) { |
2277 |
/// std::cerr << "Failure, cannot find graph" << std::endl; |
|
2310 |
/// std::cerr << "Failure, cannot find graph." << std::endl; |
|
2278 | 2311 |
/// return -1; |
2279 | 2312 |
/// } |
2280 |
/// std::cout << "The name of the default node section |
|
2313 |
/// std::cout << "The name of the default node section: " |
|
2281 | 2314 |
/// << contents.nodeSection(0) << std::endl; |
2282 |
/// std::cout << "The number of the arc maps |
|
2315 |
/// std::cout << "The number of the arc maps: " |
|
2283 | 2316 |
/// << contents.arcMaps(0).size() << std::endl; |
2284 |
/// std::cout << "The name of second arc map |
|
2317 |
/// std::cout << "The name of second arc map: " |
|
2285 | 2318 |
/// << contents.arcMaps(0)[1] << std::endl; |
2286 | 2319 |
///\endcode |
2287 | 2320 |
class LgfContents { |
2288 | 2321 |
private: |
2289 | 2322 |
|
2290 | 2323 |
std::istream* _is; |
2291 | 2324 |
bool local_is; |
2292 | 2325 |
|
2293 | 2326 |
std::vector<std::string> _node_sections; |
2294 | 2327 |
std::vector<std::string> _edge_sections; |
2295 | 2328 |
std::vector<std::string> _attribute_sections; |
2296 | 2329 |
std::vector<std::string> _extra_sections; |
... | ... |
@@ -2343,54 +2376,54 @@ |
2343 | 2376 |
|
2344 | 2377 |
|
2345 | 2378 |
/// \name Node sections |
2346 | 2379 |
/// @{ |
2347 | 2380 |
|
2348 | 2381 |
/// \brief Gives back the number of node sections in the file. |
2349 | 2382 |
/// |
2350 | 2383 |
/// Gives back the number of node sections in the file. |
2351 | 2384 |
int nodeSectionNum() const { |
2352 | 2385 |
return _node_sections.size(); |
2353 | 2386 |
} |
2354 | 2387 |
|
2355 |
/// \brief Returns the section name at the given position. |
|
2388 |
/// \brief Returns the node section name at the given position. |
|
2356 | 2389 |
/// |
2357 |
/// Returns the section name at the given position. |
|
2390 |
/// Returns the node section name at the given position. |
|
2358 | 2391 |
const std::string& nodeSection(int i) const { |
2359 | 2392 |
return _node_sections[i]; |
2360 | 2393 |
} |
2361 | 2394 |
|
2362 | 2395 |
/// \brief Gives back the node maps for the given section. |
2363 | 2396 |
/// |
2364 | 2397 |
/// Gives back the node maps for the given section. |
2365 | 2398 |
const std::vector<std::string>& nodeMapNames(int i) const { |
2366 | 2399 |
return _node_maps[i]; |
2367 | 2400 |
} |
2368 | 2401 |
|
2369 | 2402 |
/// @} |
2370 | 2403 |
|
2371 | 2404 |
/// \name Arc/Edge sections |
2372 | 2405 |
/// @{ |
2373 | 2406 |
|
2374 | 2407 |
/// \brief Gives back the number of arc/edge sections in the file. |
2375 | 2408 |
/// |
2376 | 2409 |
/// Gives back the number of arc/edge sections in the file. |
2377 | 2410 |
/// \note It is synonym of \c edgeSectionNum(). |
2378 | 2411 |
int arcSectionNum() const { |
2379 | 2412 |
return _edge_sections.size(); |
2380 | 2413 |
} |
2381 | 2414 |
|
2382 |
/// \brief Returns the section name at the given position. |
|
2415 |
/// \brief Returns the arc/edge section name at the given position. |
|
2383 | 2416 |
/// |
2384 |
/// Returns the section name at the given position. |
|
2417 |
/// Returns the arc/edge section name at the given position. |
|
2385 | 2418 |
/// \note It is synonym of \c edgeSection(). |
2386 | 2419 |
const std::string& arcSection(int i) const { |
2387 | 2420 |
return _edge_sections[i]; |
2388 | 2421 |
} |
2389 | 2422 |
|
2390 | 2423 |
/// \brief Gives back the arc/edge maps for the given section. |
2391 | 2424 |
/// |
2392 | 2425 |
/// Gives back the arc/edge maps for the given section. |
2393 | 2426 |
/// \note It is synonym of \c edgeMapNames(). |
2394 | 2427 |
const std::vector<std::string>& arcMapNames(int i) const { |
2395 | 2428 |
return _edge_maps[i]; |
2396 | 2429 |
} |
... | ... |
@@ -2427,27 +2460,27 @@ |
2427 | 2460 |
/// @} |
2428 | 2461 |
|
2429 | 2462 |
/// \name Attribute sections |
2430 | 2463 |
/// @{ |
2431 | 2464 |
|
2432 | 2465 |
/// \brief Gives back the number of attribute sections in the file. |
2433 | 2466 |
/// |
2434 | 2467 |
/// Gives back the number of attribute sections in the file. |
2435 | 2468 |
int attributeSectionNum() const { |
2436 | 2469 |
return _attribute_sections.size(); |
2437 | 2470 |
} |
2438 | 2471 |
|
2439 |
/// \brief Returns the section name at the given position. |
|
2472 |
/// \brief Returns the attribute section name at the given position. |
|
2440 | 2473 |
/// |
2441 |
/// Returns the section name at the given position. |
|
2474 |
/// Returns the attribute section name at the given position. |
|
2442 | 2475 |
const std::string& attributeSectionNames(int i) const { |
2443 | 2476 |
return _attribute_sections[i]; |
2444 | 2477 |
} |
2445 | 2478 |
|
2446 | 2479 |
/// \brief Gives back the attributes for the given section. |
2447 | 2480 |
/// |
2448 | 2481 |
/// Gives back the attributes for the given section. |
2449 | 2482 |
const std::vector<std::string>& attributes(int i) const { |
2450 | 2483 |
return _attributes[i]; |
2451 | 2484 |
} |
2452 | 2485 |
|
2453 | 2486 |
/// @} |
... | ... |
@@ -2520,27 +2553,27 @@ |
2520 | 2553 |
_reader_bits::readToken(line, attr); |
2521 | 2554 |
attrs.push_back(attr); |
2522 | 2555 |
readLine(); |
2523 | 2556 |
} |
2524 | 2557 |
line.putback(c); |
2525 | 2558 |
} |
2526 | 2559 |
|
2527 | 2560 |
public: |
2528 | 2561 |
|
2529 | 2562 |
/// \name Execution of the contents reader |
2530 | 2563 |
/// @{ |
2531 | 2564 |
|
2532 |
/// \brief |
|
2565 |
/// \brief Starts the reading |
|
2533 | 2566 |
/// |
2534 |
/// This function starts the reading |
|
2567 |
/// This function starts the reading. |
|
2535 | 2568 |
void run() { |
2536 | 2569 |
|
2537 | 2570 |
readLine(); |
2538 | 2571 |
skipSection(); |
2539 | 2572 |
|
2540 | 2573 |
while (readSuccess()) { |
2541 | 2574 |
|
2542 | 2575 |
char c; |
2543 | 2576 |
line >> c; |
2544 | 2577 |
|
2545 | 2578 |
std::string section, caption; |
2546 | 2579 |
_reader_bits::readToken(line, section); |
... | ... |
@@ -9,25 +9,25 @@ |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
///\ingroup lemon_io |
20 | 20 |
///\file |
21 |
///\brief Lemon Graph Format writer. |
|
21 |
///\brief \ref lgf-format "Lemon Graph Format" writer. |
|
22 | 22 |
|
23 | 23 |
|
24 | 24 |
#ifndef LEMON_LGF_WRITER_H |
25 | 25 |
#define LEMON_LGF_WRITER_H |
26 | 26 |
|
27 | 27 |
#include <iostream> |
28 | 28 |
#include <fstream> |
29 | 29 |
#include <sstream> |
30 | 30 |
|
31 | 31 |
#include <algorithm> |
32 | 32 |
|
33 | 33 |
#include <vector> |
... | ... |
@@ -313,50 +313,50 @@ |
313 | 313 |
const Digraph& digraph); |
314 | 314 |
|
315 | 315 |
template <typename Digraph> |
316 | 316 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
317 | 317 |
const Digraph& digraph); |
318 | 318 |
|
319 | 319 |
template <typename Digraph> |
320 | 320 |
DigraphWriter<Digraph> digraphWriter(const char *fn, |
321 | 321 |
const Digraph& digraph); |
322 | 322 |
|
323 | 323 |
/// \ingroup lemon_io |
324 | 324 |
/// |
325 |
/// \brief LGF writer for directed graphs |
|
325 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
|
326 | 326 |
/// |
327 | 327 |
/// This utility writes an \ref lgf-format "LGF" file. |
328 | 328 |
/// |
329 | 329 |
/// The writing method does a batch processing. The user creates a |
330 | 330 |
/// writer object, then various writing rules can be added to the |
331 | 331 |
/// writer, and eventually the writing is executed with the \c run() |
332 | 332 |
/// member function. A map writing rule can be added to the writer |
333 | 333 |
/// with the \c nodeMap() or \c arcMap() members. An optional |
334 | 334 |
/// converter parameter can also be added as a standard functor |
335 |
/// converting from the value type of the map to std::string. If it |
|
336 |
/// is set, it will determine how the map's value type is written to |
|
335 |
/// converting from the value type of the map to \c std::string. If it |
|
336 |
/// is set, it will determine how the value type of the map is written to |
|
337 | 337 |
/// the output stream. If the functor is not set, then a default |
338 | 338 |
/// conversion will be used. The \c attribute(), \c node() and \c |
339 | 339 |
/// arc() functions are used to add attribute writing rules. |
340 | 340 |
/// |
341 | 341 |
///\code |
342 |
/// DigraphWriter<Digraph>(std::cout, digraph). |
|
343 |
/// nodeMap("coordinates", coord_map). |
|
344 |
/// nodeMap("size", size). |
|
345 |
/// nodeMap("title", title). |
|
346 |
/// arcMap("capacity", cap_map). |
|
347 |
/// node("source", src). |
|
348 |
/// node("target", trg). |
|
349 |
/// attribute("caption", caption). |
|
350 |
/// |
|
342 |
/// DigraphWriter<Digraph>(std::cout, digraph). |
|
343 |
/// nodeMap("coordinates", coord_map). |
|
344 |
/// nodeMap("size", size). |
|
345 |
/// nodeMap("title", title). |
|
346 |
/// arcMap("capacity", cap_map). |
|
347 |
/// node("source", src). |
|
348 |
/// node("target", trg). |
|
349 |
/// attribute("caption", caption). |
|
350 |
/// run(); |
|
351 | 351 |
///\endcode |
352 | 352 |
/// |
353 | 353 |
/// |
354 | 354 |
/// By default, the writer does not write additional captions to the |
355 | 355 |
/// sections, but they can be give as an optional parameter of |
356 | 356 |
/// the \c nodes(), \c arcs() or \c |
357 | 357 |
/// attributes() functions. |
358 | 358 |
/// |
359 | 359 |
/// The \c skipNodes() and \c skipArcs() functions forbid the |
360 | 360 |
/// writing of the sections. If two arc sections should be written |
361 | 361 |
/// to the output, it can be done in two passes, the first pass |
362 | 362 |
/// writes the node section and the first arc section, then the |
... | ... |
@@ -477,27 +477,27 @@ |
477 | 477 |
_nodes_caption = other._nodes_caption; |
478 | 478 |
_arcs_caption = other._arcs_caption; |
479 | 479 |
_attributes_caption = other._attributes_caption; |
480 | 480 |
} |
481 | 481 |
|
482 | 482 |
DigraphWriter& operator=(const DigraphWriter&); |
483 | 483 |
|
484 | 484 |
public: |
485 | 485 |
|
486 | 486 |
/// \name Writing rules |
487 | 487 |
/// @{ |
488 | 488 |
|
489 |
/// \brief Node map |
|
489 |
/// \brief Node map writing rule |
|
490 | 490 |
/// |
491 |
/// Add a node map |
|
491 |
/// Add a node map writing rule to the writer. |
|
492 | 492 |
template <typename Map> |
493 | 493 |
DigraphWriter& nodeMap(const std::string& caption, const Map& map) { |
494 | 494 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
495 | 495 |
_writer_bits::MapStorageBase<Node>* storage = |
496 | 496 |
new _writer_bits::MapStorage<Node, Map>(map); |
497 | 497 |
_node_maps.push_back(std::make_pair(caption, storage)); |
498 | 498 |
return *this; |
499 | 499 |
} |
500 | 500 |
|
501 | 501 |
/// \brief Node map writing rule |
502 | 502 |
/// |
503 | 503 |
/// Add a node map writing rule with specialized converter to the |
... | ... |
@@ -577,66 +577,66 @@ |
577 | 577 |
/// \brief Arc writing rule |
578 | 578 |
/// |
579 | 579 |
/// Add an arc writing rule to writer. |
580 | 580 |
DigraphWriter& arc(const std::string& caption, const Arc& arc) { |
581 | 581 |
typedef _writer_bits::MapLookUpConverter<Arc> Converter; |
582 | 582 |
Converter converter(_arc_index); |
583 | 583 |
_writer_bits::ValueStorageBase* storage = |
584 | 584 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
585 | 585 |
_attributes.push_back(std::make_pair(caption, storage)); |
586 | 586 |
return *this; |
587 | 587 |
} |
588 | 588 |
|
589 |
/// \name |
|
589 |
/// \name Section captions |
|
590 | 590 |
/// @{ |
591 | 591 |
|
592 |
/// \brief |
|
592 |
/// \brief Add an additional caption to the \c \@nodes section |
|
593 | 593 |
/// |
594 |
/// |
|
594 |
/// Add an additional caption to the \c \@nodes section. |
|
595 | 595 |
DigraphWriter& nodes(const std::string& caption) { |
596 | 596 |
_nodes_caption = caption; |
597 | 597 |
return *this; |
598 | 598 |
} |
599 | 599 |
|
600 |
/// \brief |
|
600 |
/// \brief Add an additional caption to the \c \@arcs section |
|
601 | 601 |
/// |
602 |
/// |
|
602 |
/// Add an additional caption to the \c \@arcs section. |
|
603 | 603 |
DigraphWriter& arcs(const std::string& caption) { |
604 | 604 |
_arcs_caption = caption; |
605 | 605 |
return *this; |
606 | 606 |
} |
607 | 607 |
|
608 |
/// \brief |
|
608 |
/// \brief Add an additional caption to the \c \@attributes section |
|
609 | 609 |
/// |
610 |
/// |
|
610 |
/// Add an additional caption to the \c \@attributes section. |
|
611 | 611 |
DigraphWriter& attributes(const std::string& caption) { |
612 | 612 |
_attributes_caption = caption; |
613 | 613 |
return *this; |
614 | 614 |
} |
615 | 615 |
|
616 | 616 |
/// \name Skipping section |
617 | 617 |
/// @{ |
618 | 618 |
|
619 | 619 |
/// \brief Skip writing the node set |
620 | 620 |
/// |
621 |
/// The \c \@nodes section will |
|
621 |
/// The \c \@nodes section will not be written to the stream. |
|
622 | 622 |
DigraphWriter& skipNodes() { |
623 | 623 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
624 | 624 |
_skip_nodes = true; |
625 | 625 |
return *this; |
626 | 626 |
} |
627 | 627 |
|
628 | 628 |
/// \brief Skip writing arc set |
629 | 629 |
/// |
630 |
/// The \c \@arcs section will |
|
630 |
/// The \c \@arcs section will not be written to the stream. |
|
631 | 631 |
DigraphWriter& skipArcs() { |
632 | 632 |
LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member"); |
633 | 633 |
_skip_arcs = true; |
634 | 634 |
return *this; |
635 | 635 |
} |
636 | 636 |
|
637 | 637 |
/// @} |
638 | 638 |
|
639 | 639 |
private: |
640 | 640 |
|
641 | 641 |
void writeNodes() { |
642 | 642 |
_writer_bits::MapStorageBase<Node>* label = 0; |
... | ... |
@@ -826,90 +826,103 @@ |
826 | 826 |
_writer_bits::writeToken(*_os, it->second->get()); |
827 | 827 |
*_os << std::endl; |
828 | 828 |
} |
829 | 829 |
} |
830 | 830 |
|
831 | 831 |
public: |
832 | 832 |
|
833 | 833 |
/// \name Execution of the writer |
834 | 834 |
/// @{ |
835 | 835 |
|
836 | 836 |
/// \brief Start the batch processing |
837 | 837 |
/// |
838 |
/// This function starts the batch processing |
|
838 |
/// This function starts the batch processing. |
|
839 | 839 |
void run() { |
840 | 840 |
if (!_skip_nodes) { |
841 | 841 |
writeNodes(); |
842 | 842 |
} else { |
843 | 843 |
createNodeIndex(); |
844 | 844 |
} |
845 | 845 |
if (!_skip_arcs) { |
846 | 846 |
writeArcs(); |
847 | 847 |
} else { |
848 | 848 |
createArcIndex(); |
849 | 849 |
} |
850 | 850 |
writeAttributes(); |
851 | 851 |
} |
852 | 852 |
|
853 |
/// \brief |
|
853 |
/// \brief Give back the stream of the writer |
|
854 | 854 |
/// |
855 |
/// |
|
855 |
/// Give back the stream of the writer. |
|
856 | 856 |
std::ostream& ostream() { |
857 | 857 |
return *_os; |
858 | 858 |
} |
859 | 859 |
|
860 | 860 |
/// @} |
861 | 861 |
}; |
862 | 862 |
|
863 |
/// \brief Return a \ref DigraphWriter class |
|
864 |
/// |
|
865 |
/// This function just returns a \ref DigraphWriter class. |
|
863 | 866 |
/// \relates DigraphWriter |
864 | 867 |
template <typename Digraph> |
865 | 868 |
DigraphWriter<Digraph> digraphWriter(std::ostream& os, |
866 | 869 |
const Digraph& digraph) { |
867 | 870 |
DigraphWriter<Digraph> tmp(os, digraph); |
868 | 871 |
return tmp; |
869 | 872 |
} |
870 | 873 |
|
874 |
/// \brief Return a \ref DigraphWriter class |
|
875 |
/// |
|
876 |
/// This function just returns a \ref DigraphWriter class. |
|
871 | 877 |
/// \relates DigraphWriter |
872 | 878 |
template <typename Digraph> |
873 | 879 |
DigraphWriter<Digraph> digraphWriter(const std::string& fn, |
874 | 880 |
const Digraph& digraph) { |
875 | 881 |
DigraphWriter<Digraph> tmp(fn, digraph); |
876 | 882 |
return tmp; |
877 | 883 |
} |
878 | 884 |
|
885 |
/// \brief Return a \ref DigraphWriter class |
|
886 |
/// |
|
887 |
/// This function just returns a \ref DigraphWriter class. |
|
879 | 888 |
/// \relates DigraphWriter |
880 | 889 |
template <typename Digraph> |
881 | 890 |
DigraphWriter<Digraph> digraphWriter(const char* fn, |
882 | 891 |
const Digraph& digraph) { |
883 | 892 |
DigraphWriter<Digraph> tmp(fn, digraph); |
884 | 893 |
return tmp; |
885 | 894 |
} |
886 | 895 |
|
887 | 896 |
template <typename Graph> |
888 | 897 |
class GraphWriter; |
889 | 898 |
|
890 | 899 |
template <typename Graph> |
891 | 900 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph); |
892 | 901 |
|
893 | 902 |
template <typename Graph> |
894 | 903 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph); |
895 | 904 |
|
896 | 905 |
template <typename Graph> |
897 | 906 |
GraphWriter<Graph> graphWriter(const char *fn, const Graph& graph); |
898 | 907 |
|
899 | 908 |
/// \ingroup lemon_io |
900 | 909 |
/// |
901 |
/// \brief LGF writer for directed graphs |
|
910 |
/// \brief \ref lgf-format "LGF" writer for directed graphs |
|
902 | 911 |
/// |
903 | 912 |
/// This utility writes an \ref lgf-format "LGF" file. |
913 |
/// |
|
914 |
/// It can be used almost the same way as \c DigraphWriter. |
|
915 |
/// The only difference is that this class can handle edges and |
|
916 |
/// edge maps as well as arcs and arc maps. |
|
904 | 917 |
template <typename _Graph> |
905 | 918 |
class GraphWriter { |
906 | 919 |
public: |
907 | 920 |
|
908 | 921 |
typedef _Graph Graph; |
909 | 922 |
TEMPLATE_GRAPH_TYPEDEFS(Graph); |
910 | 923 |
|
911 | 924 |
private: |
912 | 925 |
|
913 | 926 |
|
914 | 927 |
std::ostream* _os; |
915 | 928 |
bool local_os; |
... | ... |
@@ -1014,27 +1027,27 @@ |
1014 | 1027 |
_nodes_caption = other._nodes_caption; |
1015 | 1028 |
_edges_caption = other._edges_caption; |
1016 | 1029 |
_attributes_caption = other._attributes_caption; |
1017 | 1030 |
} |
1018 | 1031 |
|
1019 | 1032 |
GraphWriter& operator=(const GraphWriter&); |
1020 | 1033 |
|
1021 | 1034 |
public: |
1022 | 1035 |
|
1023 | 1036 |
/// \name Writing rules |
1024 | 1037 |
/// @{ |
1025 | 1038 |
|
1026 |
/// \brief Node map |
|
1039 |
/// \brief Node map writing rule |
|
1027 | 1040 |
/// |
1028 |
/// Add a node map |
|
1041 |
/// Add a node map writing rule to the writer. |
|
1029 | 1042 |
template <typename Map> |
1030 | 1043 |
GraphWriter& nodeMap(const std::string& caption, const Map& map) { |
1031 | 1044 |
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>(); |
1032 | 1045 |
_writer_bits::MapStorageBase<Node>* storage = |
1033 | 1046 |
new _writer_bits::MapStorage<Node, Map>(map); |
1034 | 1047 |
_node_maps.push_back(std::make_pair(caption, storage)); |
1035 | 1048 |
return *this; |
1036 | 1049 |
} |
1037 | 1050 |
|
1038 | 1051 |
/// \brief Node map writing rule |
1039 | 1052 |
/// |
1040 | 1053 |
/// Add a node map writing rule with specialized converter to the |
... | ... |
@@ -1160,66 +1173,66 @@ |
1160 | 1173 |
/// \brief Arc writing rule |
1161 | 1174 |
/// |
1162 | 1175 |
/// Add an arc writing rule to writer. |
1163 | 1176 |
GraphWriter& arc(const std::string& caption, const Arc& arc) { |
1164 | 1177 |
typedef _writer_bits::GraphArcLookUpConverter<Graph> Converter; |
1165 | 1178 |
Converter converter(_graph, _edge_index); |
1166 | 1179 |
_writer_bits::ValueStorageBase* storage = |
1167 | 1180 |
new _writer_bits::ValueStorage<Arc, Converter>(arc, converter); |
1168 | 1181 |
_attributes.push_back(std::make_pair(caption, storage)); |
1169 | 1182 |
return *this; |
1170 | 1183 |
} |
1171 | 1184 |
|
1172 |
/// \name |
|
1185 |
/// \name Section captions |
|
1173 | 1186 |
/// @{ |
1174 | 1187 |
|
1175 |
/// \brief |
|
1188 |
/// \brief Add an additional caption to the \c \@nodes section |
|
1176 | 1189 |
/// |
1177 |
/// |
|
1190 |
/// Add an additional caption to the \c \@nodes section. |
|
1178 | 1191 |
GraphWriter& nodes(const std::string& caption) { |
1179 | 1192 |
_nodes_caption = caption; |
1180 | 1193 |
return *this; |
1181 | 1194 |
} |
1182 | 1195 |
|
1183 |
/// \brief |
|
1196 |
/// \brief Add an additional caption to the \c \@arcs section |
|
1184 | 1197 |
/// |
1185 |
/// |
|
1198 |
/// Add an additional caption to the \c \@arcs section. |
|
1186 | 1199 |
GraphWriter& edges(const std::string& caption) { |
1187 | 1200 |
_edges_caption = caption; |
1188 | 1201 |
return *this; |
1189 | 1202 |
} |
1190 | 1203 |
|
1191 |
/// \brief |
|
1204 |
/// \brief Add an additional caption to the \c \@attributes section |
|
1192 | 1205 |
/// |
1193 |
/// |
|
1206 |
/// Add an additional caption to the \c \@attributes section. |
|
1194 | 1207 |
GraphWriter& attributes(const std::string& caption) { |
1195 | 1208 |
_attributes_caption = caption; |
1196 | 1209 |
return *this; |
1197 | 1210 |
} |
1198 | 1211 |
|
1199 | 1212 |
/// \name Skipping section |
1200 | 1213 |
/// @{ |
1201 | 1214 |
|
1202 | 1215 |
/// \brief Skip writing the node set |
1203 | 1216 |
/// |
1204 |
/// The \c \@nodes section will |
|
1217 |
/// The \c \@nodes section will not be written to the stream. |
|
1205 | 1218 |
GraphWriter& skipNodes() { |
1206 | 1219 |
LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member"); |
1207 | 1220 |
_skip_nodes = true; |
1208 | 1221 |
return *this; |
1209 | 1222 |
} |
1210 | 1223 |
|
1211 | 1224 |
/// \brief Skip writing edge set |
1212 | 1225 |
/// |
1213 |
/// The \c \@edges section will |
|
1226 |
/// The \c \@edges section will not be written to the stream. |
|
1214 | 1227 |
GraphWriter& skipEdges() { |
1215 | 1228 |
LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member"); |
1216 | 1229 |
_skip_edges = true; |
1217 | 1230 |
return *this; |
1218 | 1231 |
} |
1219 | 1232 |
|
1220 | 1233 |
/// @} |
1221 | 1234 |
|
1222 | 1235 |
private: |
1223 | 1236 |
|
1224 | 1237 |
void writeNodes() { |
1225 | 1238 |
_writer_bits::MapStorageBase<Node>* label = 0; |
... | ... |
@@ -1409,60 +1422,69 @@ |
1409 | 1422 |
_writer_bits::writeToken(*_os, it->second->get()); |
1410 | 1423 |
*_os << std::endl; |
1411 | 1424 |
} |
1412 | 1425 |
} |
1413 | 1426 |
|
1414 | 1427 |
public: |
1415 | 1428 |
|
1416 | 1429 |
/// \name Execution of the writer |
1417 | 1430 |
/// @{ |
1418 | 1431 |
|
1419 | 1432 |
/// \brief Start the batch processing |
1420 | 1433 |
/// |
1421 |
/// This function starts the batch processing |
|
1434 |
/// This function starts the batch processing. |
|
1422 | 1435 |
void run() { |
1423 | 1436 |
if (!_skip_nodes) { |
1424 | 1437 |
writeNodes(); |
1425 | 1438 |
} else { |
1426 | 1439 |
createNodeIndex(); |
1427 | 1440 |
} |
1428 | 1441 |
if (!_skip_edges) { |
1429 | 1442 |
writeEdges(); |
1430 | 1443 |
} else { |
1431 | 1444 |
createEdgeIndex(); |
1432 | 1445 |
} |
1433 | 1446 |
writeAttributes(); |
1434 | 1447 |
} |
1435 | 1448 |
|
1436 |
/// \brief |
|
1449 |
/// \brief Give back the stream of the writer |
|
1437 | 1450 |
/// |
1438 |
/// |
|
1451 |
/// Give back the stream of the writer |
|
1439 | 1452 |
std::ostream& ostream() { |
1440 | 1453 |
return *_os; |
1441 | 1454 |
} |
1442 | 1455 |
|
1443 | 1456 |
/// @} |
1444 | 1457 |
}; |
1445 | 1458 |
|
1459 |
/// \brief Return a \ref GraphWriter class |
|
1460 |
/// |
|
1461 |
/// This function just returns a \ref GraphWriter class. |
|
1446 | 1462 |
/// \relates GraphWriter |
1447 | 1463 |
template <typename Graph> |
1448 | 1464 |
GraphWriter<Graph> graphWriter(std::ostream& os, const Graph& graph) { |
1449 | 1465 |
GraphWriter<Graph> tmp(os, graph); |
1450 | 1466 |
return tmp; |
1451 | 1467 |
} |
1452 | 1468 |
|
1469 |
/// \brief Return a \ref GraphWriter class |
|
1470 |
/// |
|
1471 |
/// This function just returns a \ref GraphWriter class. |
|
1453 | 1472 |
/// \relates GraphWriter |
1454 | 1473 |
template <typename Graph> |
1455 | 1474 |
GraphWriter<Graph> graphWriter(const std::string& fn, const Graph& graph) { |
1456 | 1475 |
GraphWriter<Graph> tmp(fn, graph); |
1457 | 1476 |
return tmp; |
1458 | 1477 |
} |
1459 | 1478 |
|
1479 |
/// \brief Return a \ref GraphWriter class |
|
1480 |
/// |
|
1481 |
/// This function just returns a \ref GraphWriter class. |
|
1460 | 1482 |
/// \relates GraphWriter |
1461 | 1483 |
template <typename Graph> |
1462 | 1484 |
GraphWriter<Graph> graphWriter(const char* fn, const Graph& graph) { |
1463 | 1485 |
GraphWriter<Graph> tmp(fn, graph); |
1464 | 1486 |
return tmp; |
1465 | 1487 |
} |
1466 | 1488 |
} |
1467 | 1489 |
|
1468 | 1490 |
#endif |
0 comments (0 inline)