... |
... |
@@ -423,205 +423,205 @@
|
423 |
423 |
///\code
|
424 |
424 |
/// DigraphReader<Digraph>(std::cin, digraph).
|
425 |
425 |
/// nodeMap("coordinates", coord_map).
|
426 |
426 |
/// arcMap("capacity", cap_map).
|
427 |
427 |
/// node("source", src).
|
428 |
428 |
/// node("target", trg).
|
429 |
429 |
/// attribute("caption", caption).
|
430 |
430 |
/// run();
|
431 |
431 |
///\endcode
|
432 |
432 |
///
|
433 |
433 |
/// By default the reader uses the first section in the file of the
|
434 |
434 |
/// proper type. If a section has an optional name, then it can be
|
435 |
435 |
/// selected for reading by giving an optional name parameter to the
|
436 |
436 |
/// \c nodes(), \c arcs() or \c attributes() functions.
|
437 |
437 |
///
|
438 |
438 |
/// The \c useNodes() and \c useArcs() functions are used to tell the reader
|
439 |
439 |
/// that the nodes or arcs should not be constructed (added to the
|
440 |
440 |
/// graph) during the reading, but instead the label map of the items
|
441 |
441 |
/// are given as a parameter of these functions. An
|
442 |
442 |
/// application of these functions is multipass reading, which is
|
443 |
443 |
/// important if two \c \@arcs sections must be read from the
|
444 |
444 |
/// file. In this case the first phase would read the node set and one
|
445 |
445 |
/// of the arc sets, while the second phase would read the second arc
|
446 |
446 |
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
|
447 |
447 |
/// The previously read label node map should be passed to the \c
|
448 |
448 |
/// useNodes() functions. Another application of multipass reading when
|
449 |
449 |
/// paths are given as a node map or an arc map.
|
450 |
450 |
/// It is impossible to read this in
|
451 |
451 |
/// a single pass, because the arcs are not constructed when the node
|
452 |
452 |
/// maps are read.
|
453 |
453 |
template <typename _Digraph>
|
454 |
454 |
class DigraphReader {
|
455 |
455 |
public:
|
456 |
456 |
|
457 |
457 |
typedef _Digraph Digraph;
|
458 |
458 |
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
|
459 |
459 |
|
460 |
460 |
private:
|
461 |
461 |
|
462 |
462 |
|
463 |
463 |
std::istream* _is;
|
464 |
464 |
bool local_is;
|
465 |
465 |
std::string _filename;
|
466 |
466 |
|
467 |
467 |
Digraph& _digraph;
|
468 |
468 |
|
469 |
469 |
std::string _nodes_caption;
|
470 |
470 |
std::string _arcs_caption;
|
471 |
471 |
std::string _attributes_caption;
|
472 |
472 |
|
473 |
473 |
typedef std::map<std::string, Node> NodeIndex;
|
474 |
474 |
NodeIndex _node_index;
|
475 |
475 |
typedef std::map<std::string, Arc> ArcIndex;
|
476 |
476 |
ArcIndex _arc_index;
|
477 |
477 |
|
478 |
478 |
typedef std::vector<std::pair<std::string,
|
479 |
479 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
|
480 |
480 |
NodeMaps _node_maps;
|
481 |
481 |
|
482 |
482 |
typedef std::vector<std::pair<std::string,
|
483 |
483 |
_reader_bits::MapStorageBase<Arc>*> >ArcMaps;
|
484 |
484 |
ArcMaps _arc_maps;
|
485 |
485 |
|
486 |
486 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
|
487 |
487 |
Attributes;
|
488 |
488 |
Attributes _attributes;
|
489 |
489 |
|
490 |
490 |
bool _use_nodes;
|
491 |
491 |
bool _use_arcs;
|
492 |
492 |
|
493 |
493 |
bool _skip_nodes;
|
494 |
494 |
bool _skip_arcs;
|
495 |
495 |
|
496 |
496 |
int line_num;
|
497 |
497 |
std::istringstream line;
|
498 |
498 |
|
499 |
499 |
public:
|
500 |
500 |
|
501 |
501 |
/// \brief Constructor
|
502 |
502 |
///
|
503 |
503 |
/// Construct a directed graph reader, which reads from the given
|
504 |
504 |
/// input stream.
|
505 |
505 |
DigraphReader(std::istream& is, Digraph& digraph)
|
506 |
506 |
: _is(&is), local_is(false), _digraph(digraph),
|
507 |
507 |
_use_nodes(false), _use_arcs(false),
|
508 |
508 |
_skip_nodes(false), _skip_arcs(false) {}
|
509 |
509 |
|
510 |
510 |
/// \brief Constructor
|
511 |
511 |
///
|
512 |
512 |
/// Construct a directed graph reader, which reads from the given
|
513 |
513 |
/// file.
|
514 |
514 |
DigraphReader(const std::string& fn, Digraph& digraph)
|
515 |
515 |
: _is(new std::ifstream(fn.c_str())), local_is(true),
|
516 |
516 |
_filename(fn), _digraph(digraph),
|
517 |
517 |
_use_nodes(false), _use_arcs(false),
|
518 |
518 |
_skip_nodes(false), _skip_arcs(false) {
|
519 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
519 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
520 |
520 |
}
|
521 |
521 |
|
522 |
522 |
/// \brief Constructor
|
523 |
523 |
///
|
524 |
524 |
/// Construct a directed graph reader, which reads from the given
|
525 |
525 |
/// file.
|
526 |
526 |
DigraphReader(const char* fn, Digraph& digraph)
|
527 |
527 |
: _is(new std::ifstream(fn)), local_is(true),
|
528 |
528 |
_filename(fn), _digraph(digraph),
|
529 |
529 |
_use_nodes(false), _use_arcs(false),
|
530 |
530 |
_skip_nodes(false), _skip_arcs(false) {
|
531 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
531 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
532 |
532 |
}
|
533 |
533 |
|
534 |
534 |
/// \brief Destructor
|
535 |
535 |
~DigraphReader() {
|
536 |
536 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
537 |
537 |
it != _node_maps.end(); ++it) {
|
538 |
538 |
delete it->second;
|
539 |
539 |
}
|
540 |
540 |
|
541 |
541 |
for (typename ArcMaps::iterator it = _arc_maps.begin();
|
542 |
542 |
it != _arc_maps.end(); ++it) {
|
543 |
543 |
delete it->second;
|
544 |
544 |
}
|
545 |
545 |
|
546 |
546 |
for (typename Attributes::iterator it = _attributes.begin();
|
547 |
547 |
it != _attributes.end(); ++it) {
|
548 |
548 |
delete it->second;
|
549 |
549 |
}
|
550 |
550 |
|
551 |
551 |
if (local_is) {
|
552 |
552 |
delete _is;
|
553 |
553 |
}
|
554 |
554 |
|
555 |
555 |
}
|
556 |
556 |
|
557 |
557 |
private:
|
558 |
558 |
|
559 |
559 |
friend DigraphReader<Digraph> digraphReader<>(std::istream& is,
|
560 |
560 |
Digraph& digraph);
|
561 |
561 |
friend DigraphReader<Digraph> digraphReader<>(const std::string& fn,
|
562 |
562 |
Digraph& digraph);
|
563 |
563 |
friend DigraphReader<Digraph> digraphReader<>(const char *fn,
|
564 |
564 |
Digraph& digraph);
|
565 |
565 |
|
566 |
566 |
DigraphReader(DigraphReader& other)
|
567 |
567 |
: _is(other._is), local_is(other.local_is), _digraph(other._digraph),
|
568 |
568 |
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
|
569 |
569 |
_skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
|
570 |
570 |
|
571 |
571 |
other._is = 0;
|
572 |
572 |
other.local_is = false;
|
573 |
573 |
|
574 |
574 |
_node_index.swap(other._node_index);
|
575 |
575 |
_arc_index.swap(other._arc_index);
|
576 |
576 |
|
577 |
577 |
_node_maps.swap(other._node_maps);
|
578 |
578 |
_arc_maps.swap(other._arc_maps);
|
579 |
579 |
_attributes.swap(other._attributes);
|
580 |
580 |
|
581 |
581 |
_nodes_caption = other._nodes_caption;
|
582 |
582 |
_arcs_caption = other._arcs_caption;
|
583 |
583 |
_attributes_caption = other._attributes_caption;
|
584 |
584 |
|
585 |
585 |
}
|
586 |
586 |
|
587 |
587 |
DigraphReader& operator=(const DigraphReader&);
|
588 |
588 |
|
589 |
589 |
public:
|
590 |
590 |
|
591 |
591 |
/// \name Reading rules
|
592 |
592 |
/// @{
|
593 |
593 |
|
594 |
594 |
/// \brief Node map reading rule
|
595 |
595 |
///
|
596 |
596 |
/// Add a node map reading rule to the reader.
|
597 |
597 |
template <typename Map>
|
598 |
598 |
DigraphReader& nodeMap(const std::string& caption, Map& map) {
|
599 |
599 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
600 |
600 |
_reader_bits::MapStorageBase<Node>* storage =
|
601 |
601 |
new _reader_bits::MapStorage<Node, Map>(map);
|
602 |
602 |
_node_maps.push_back(std::make_pair(caption, storage));
|
603 |
603 |
return *this;
|
604 |
604 |
}
|
605 |
605 |
|
606 |
606 |
/// \brief Node map reading rule
|
607 |
607 |
///
|
608 |
608 |
/// Add a node map reading rule with specialized converter to the
|
609 |
609 |
/// reader.
|
610 |
610 |
template <typename Map, typename Converter>
|
611 |
611 |
DigraphReader& nodeMap(const std::string& caption, Map& map,
|
612 |
612 |
const Converter& converter = Converter()) {
|
613 |
613 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
614 |
614 |
_reader_bits::MapStorageBase<Node>* storage =
|
615 |
615 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
|
616 |
616 |
_node_maps.push_back(std::make_pair(caption, storage));
|
617 |
617 |
return *this;
|
618 |
618 |
}
|
619 |
619 |
|
620 |
620 |
/// \brief Arc map reading rule
|
621 |
621 |
///
|
622 |
622 |
/// Add an arc map reading rule to the reader.
|
623 |
623 |
template <typename Map>
|
624 |
624 |
DigraphReader& arcMap(const std::string& caption, Map& map) {
|
625 |
625 |
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
|
626 |
626 |
_reader_bits::MapStorageBase<Arc>* storage =
|
627 |
627 |
new _reader_bits::MapStorage<Arc, Map>(map);
|
... |
... |
@@ -786,634 +786,631 @@
|
786 |
786 |
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
|
787 |
787 |
_use_arcs = true;
|
788 |
788 |
for (ArcIt a(_digraph); a != INVALID; ++a) {
|
789 |
789 |
_arc_index.insert(std::make_pair(converter(map[a]), a));
|
790 |
790 |
}
|
791 |
791 |
return *this;
|
792 |
792 |
}
|
793 |
793 |
|
794 |
794 |
/// \brief Skips the reading of node section
|
795 |
795 |
///
|
796 |
796 |
/// Omit the reading of the node section. This implies that each node
|
797 |
797 |
/// map reading rule will be abandoned, and the nodes of the graph
|
798 |
798 |
/// will not be constructed, which usually cause that the arc set
|
799 |
799 |
/// could not be read due to lack of node name resolving.
|
800 |
800 |
/// Therefore \c skipArcs() function should also be used, or
|
801 |
801 |
/// \c useNodes() should be used to specify the label of the nodes.
|
802 |
802 |
DigraphReader& skipNodes() {
|
803 |
803 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
|
804 |
804 |
_skip_nodes = true;
|
805 |
805 |
return *this;
|
806 |
806 |
}
|
807 |
807 |
|
808 |
808 |
/// \brief Skips the reading of arc section
|
809 |
809 |
///
|
810 |
810 |
/// Omit the reading of the arc section. This implies that each arc
|
811 |
811 |
/// map reading rule will be abandoned, and the arcs of the graph
|
812 |
812 |
/// will not be constructed.
|
813 |
813 |
DigraphReader& skipArcs() {
|
814 |
814 |
LEMON_ASSERT(!_skip_arcs, "Skip arcs already set");
|
815 |
815 |
_skip_arcs = true;
|
816 |
816 |
return *this;
|
817 |
817 |
}
|
818 |
818 |
|
819 |
819 |
/// @}
|
820 |
820 |
|
821 |
821 |
private:
|
822 |
822 |
|
823 |
823 |
bool readLine() {
|
824 |
824 |
std::string str;
|
825 |
825 |
while(++line_num, std::getline(*_is, str)) {
|
826 |
826 |
line.clear(); line.str(str);
|
827 |
827 |
char c;
|
828 |
828 |
if (line >> std::ws >> c && c != '#') {
|
829 |
829 |
line.putback(c);
|
830 |
830 |
return true;
|
831 |
831 |
}
|
832 |
832 |
}
|
833 |
833 |
return false;
|
834 |
834 |
}
|
835 |
835 |
|
836 |
836 |
bool readSuccess() {
|
837 |
837 |
return static_cast<bool>(*_is);
|
838 |
838 |
}
|
839 |
839 |
|
840 |
840 |
void skipSection() {
|
841 |
841 |
char c;
|
842 |
842 |
while (readSuccess() && line >> c && c != '@') {
|
843 |
843 |
readLine();
|
844 |
844 |
}
|
845 |
845 |
line.putback(c);
|
846 |
846 |
}
|
847 |
847 |
|
848 |
848 |
void readNodes() {
|
849 |
849 |
|
850 |
850 |
std::vector<int> map_index(_node_maps.size());
|
851 |
851 |
int map_num, label_index;
|
852 |
852 |
|
853 |
853 |
char c;
|
854 |
854 |
if (!readLine() || !(line >> c) || c == '@') {
|
855 |
855 |
if (readSuccess() && line) line.putback(c);
|
856 |
856 |
if (!_node_maps.empty())
|
857 |
857 |
throw FormatError("Cannot find map names");
|
858 |
858 |
return;
|
859 |
859 |
}
|
860 |
860 |
line.putback(c);
|
861 |
861 |
|
862 |
862 |
{
|
863 |
863 |
std::map<std::string, int> maps;
|
864 |
864 |
|
865 |
865 |
std::string map;
|
866 |
866 |
int index = 0;
|
867 |
867 |
while (_reader_bits::readToken(line, map)) {
|
868 |
868 |
if (maps.find(map) != maps.end()) {
|
869 |
869 |
std::ostringstream msg;
|
870 |
870 |
msg << "Multiple occurence of node map: " << map;
|
871 |
871 |
throw FormatError(msg.str());
|
872 |
872 |
}
|
873 |
873 |
maps.insert(std::make_pair(map, index));
|
874 |
874 |
++index;
|
875 |
875 |
}
|
876 |
876 |
|
877 |
877 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
878 |
878 |
std::map<std::string, int>::iterator jt =
|
879 |
879 |
maps.find(_node_maps[i].first);
|
880 |
880 |
if (jt == maps.end()) {
|
881 |
881 |
std::ostringstream msg;
|
882 |
|
msg << "Map not found in file: " << _node_maps[i].first;
|
|
882 |
msg << "Map not found: " << _node_maps[i].first;
|
883 |
883 |
throw FormatError(msg.str());
|
884 |
884 |
}
|
885 |
885 |
map_index[i] = jt->second;
|
886 |
886 |
}
|
887 |
887 |
|
888 |
888 |
{
|
889 |
889 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
890 |
890 |
if (jt != maps.end()) {
|
891 |
891 |
label_index = jt->second;
|
892 |
892 |
} else {
|
893 |
893 |
label_index = -1;
|
894 |
894 |
}
|
895 |
895 |
}
|
896 |
896 |
map_num = maps.size();
|
897 |
897 |
}
|
898 |
898 |
|
899 |
899 |
while (readLine() && line >> c && c != '@') {
|
900 |
900 |
line.putback(c);
|
901 |
901 |
|
902 |
902 |
std::vector<std::string> tokens(map_num);
|
903 |
903 |
for (int i = 0; i < map_num; ++i) {
|
904 |
904 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
905 |
905 |
std::ostringstream msg;
|
906 |
906 |
msg << "Column not found (" << i + 1 << ")";
|
907 |
907 |
throw FormatError(msg.str());
|
908 |
908 |
}
|
909 |
909 |
}
|
910 |
910 |
if (line >> std::ws >> c)
|
911 |
|
throw FormatError("Extra character on the end of line");
|
|
911 |
throw FormatError("Extra character at the end of line");
|
912 |
912 |
|
913 |
913 |
Node n;
|
914 |
914 |
if (!_use_nodes) {
|
915 |
915 |
n = _digraph.addNode();
|
916 |
916 |
if (label_index != -1)
|
917 |
917 |
_node_index.insert(std::make_pair(tokens[label_index], n));
|
918 |
918 |
} else {
|
919 |
919 |
if (label_index == -1)
|
920 |
|
throw FormatError("Label map not found in file");
|
|
920 |
throw FormatError("Label map not found");
|
921 |
921 |
typename std::map<std::string, Node>::iterator it =
|
922 |
922 |
_node_index.find(tokens[label_index]);
|
923 |
923 |
if (it == _node_index.end()) {
|
924 |
924 |
std::ostringstream msg;
|
925 |
925 |
msg << "Node with label not found: " << tokens[label_index];
|
926 |
926 |
throw FormatError(msg.str());
|
927 |
927 |
}
|
928 |
928 |
n = it->second;
|
929 |
929 |
}
|
930 |
930 |
|
931 |
931 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
932 |
932 |
_node_maps[i].second->set(n, tokens[map_index[i]]);
|
933 |
933 |
}
|
934 |
934 |
|
935 |
935 |
}
|
936 |
936 |
if (readSuccess()) {
|
937 |
937 |
line.putback(c);
|
938 |
938 |
}
|
939 |
939 |
}
|
940 |
940 |
|
941 |
941 |
void readArcs() {
|
942 |
942 |
|
943 |
943 |
std::vector<int> map_index(_arc_maps.size());
|
944 |
944 |
int map_num, label_index;
|
945 |
945 |
|
946 |
946 |
char c;
|
947 |
947 |
if (!readLine() || !(line >> c) || c == '@') {
|
948 |
948 |
if (readSuccess() && line) line.putback(c);
|
949 |
949 |
if (!_arc_maps.empty())
|
950 |
950 |
throw FormatError("Cannot find map names");
|
951 |
951 |
return;
|
952 |
952 |
}
|
953 |
953 |
line.putback(c);
|
954 |
954 |
|
955 |
955 |
{
|
956 |
956 |
std::map<std::string, int> maps;
|
957 |
957 |
|
958 |
958 |
std::string map;
|
959 |
959 |
int index = 0;
|
960 |
960 |
while (_reader_bits::readToken(line, map)) {
|
961 |
961 |
if (maps.find(map) != maps.end()) {
|
962 |
962 |
std::ostringstream msg;
|
963 |
963 |
msg << "Multiple occurence of arc map: " << map;
|
964 |
964 |
throw FormatError(msg.str());
|
965 |
965 |
}
|
966 |
966 |
maps.insert(std::make_pair(map, index));
|
967 |
967 |
++index;
|
968 |
968 |
}
|
969 |
969 |
|
970 |
970 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
971 |
971 |
std::map<std::string, int>::iterator jt =
|
972 |
972 |
maps.find(_arc_maps[i].first);
|
973 |
973 |
if (jt == maps.end()) {
|
974 |
974 |
std::ostringstream msg;
|
975 |
|
msg << "Map not found in file: " << _arc_maps[i].first;
|
|
975 |
msg << "Map not found: " << _arc_maps[i].first;
|
976 |
976 |
throw FormatError(msg.str());
|
977 |
977 |
}
|
978 |
978 |
map_index[i] = jt->second;
|
979 |
979 |
}
|
980 |
980 |
|
981 |
981 |
{
|
982 |
982 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
983 |
983 |
if (jt != maps.end()) {
|
984 |
984 |
label_index = jt->second;
|
985 |
985 |
} else {
|
986 |
986 |
label_index = -1;
|
987 |
987 |
}
|
988 |
988 |
}
|
989 |
989 |
map_num = maps.size();
|
990 |
990 |
}
|
991 |
991 |
|
992 |
992 |
while (readLine() && line >> c && c != '@') {
|
993 |
993 |
line.putback(c);
|
994 |
994 |
|
995 |
995 |
std::string source_token;
|
996 |
996 |
std::string target_token;
|
997 |
997 |
|
998 |
998 |
if (!_reader_bits::readToken(line, source_token))
|
999 |
999 |
throw FormatError("Source not found");
|
1000 |
1000 |
|
1001 |
1001 |
if (!_reader_bits::readToken(line, target_token))
|
1002 |
1002 |
throw FormatError("Target not found");
|
1003 |
1003 |
|
1004 |
1004 |
std::vector<std::string> tokens(map_num);
|
1005 |
1005 |
for (int i = 0; i < map_num; ++i) {
|
1006 |
1006 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
1007 |
1007 |
std::ostringstream msg;
|
1008 |
1008 |
msg << "Column not found (" << i + 1 << ")";
|
1009 |
1009 |
throw FormatError(msg.str());
|
1010 |
1010 |
}
|
1011 |
1011 |
}
|
1012 |
1012 |
if (line >> std::ws >> c)
|
1013 |
|
throw FormatError("Extra character on the end of line");
|
|
1013 |
throw FormatError("Extra character at the end of line");
|
1014 |
1014 |
|
1015 |
1015 |
Arc a;
|
1016 |
1016 |
if (!_use_arcs) {
|
1017 |
1017 |
|
1018 |
1018 |
typename NodeIndex::iterator it;
|
1019 |
1019 |
|
1020 |
1020 |
it = _node_index.find(source_token);
|
1021 |
1021 |
if (it == _node_index.end()) {
|
1022 |
1022 |
std::ostringstream msg;
|
1023 |
1023 |
msg << "Item not found: " << source_token;
|
1024 |
1024 |
throw FormatError(msg.str());
|
1025 |
1025 |
}
|
1026 |
1026 |
Node source = it->second;
|
1027 |
1027 |
|
1028 |
1028 |
it = _node_index.find(target_token);
|
1029 |
1029 |
if (it == _node_index.end()) {
|
1030 |
1030 |
std::ostringstream msg;
|
1031 |
1031 |
msg << "Item not found: " << target_token;
|
1032 |
1032 |
throw FormatError(msg.str());
|
1033 |
1033 |
}
|
1034 |
1034 |
Node target = it->second;
|
1035 |
1035 |
|
1036 |
1036 |
a = _digraph.addArc(source, target);
|
1037 |
1037 |
if (label_index != -1)
|
1038 |
1038 |
_arc_index.insert(std::make_pair(tokens[label_index], a));
|
1039 |
1039 |
} else {
|
1040 |
1040 |
if (label_index == -1)
|
1041 |
|
throw FormatError("Label map not found in file");
|
|
1041 |
throw FormatError("Label map not found");
|
1042 |
1042 |
typename std::map<std::string, Arc>::iterator it =
|
1043 |
1043 |
_arc_index.find(tokens[label_index]);
|
1044 |
1044 |
if (it == _arc_index.end()) {
|
1045 |
1045 |
std::ostringstream msg;
|
1046 |
1046 |
msg << "Arc with label not found: " << tokens[label_index];
|
1047 |
1047 |
throw FormatError(msg.str());
|
1048 |
1048 |
}
|
1049 |
1049 |
a = it->second;
|
1050 |
1050 |
}
|
1051 |
1051 |
|
1052 |
1052 |
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
|
1053 |
1053 |
_arc_maps[i].second->set(a, tokens[map_index[i]]);
|
1054 |
1054 |
}
|
1055 |
1055 |
|
1056 |
1056 |
}
|
1057 |
1057 |
if (readSuccess()) {
|
1058 |
1058 |
line.putback(c);
|
1059 |
1059 |
}
|
1060 |
1060 |
}
|
1061 |
1061 |
|
1062 |
1062 |
void readAttributes() {
|
1063 |
1063 |
|
1064 |
1064 |
std::set<std::string> read_attr;
|
1065 |
1065 |
|
1066 |
1066 |
char c;
|
1067 |
1067 |
while (readLine() && line >> c && c != '@') {
|
1068 |
1068 |
line.putback(c);
|
1069 |
1069 |
|
1070 |
1070 |
std::string attr, token;
|
1071 |
1071 |
if (!_reader_bits::readToken(line, attr))
|
1072 |
1072 |
throw FormatError("Attribute name not found");
|
1073 |
1073 |
if (!_reader_bits::readToken(line, token))
|
1074 |
1074 |
throw FormatError("Attribute value not found");
|
1075 |
1075 |
if (line >> c)
|
1076 |
|
throw FormatError("Extra character on the end of line");
|
|
1076 |
throw FormatError("Extra character at the end of line");
|
1077 |
1077 |
|
1078 |
1078 |
{
|
1079 |
1079 |
std::set<std::string>::iterator it = read_attr.find(attr);
|
1080 |
1080 |
if (it != read_attr.end()) {
|
1081 |
1081 |
std::ostringstream msg;
|
1082 |
|
msg << "Multiple occurence of attribute " << attr;
|
|
1082 |
msg << "Multiple occurence of attribute: " << attr;
|
1083 |
1083 |
throw FormatError(msg.str());
|
1084 |
1084 |
}
|
1085 |
1085 |
read_attr.insert(attr);
|
1086 |
1086 |
}
|
1087 |
1087 |
|
1088 |
1088 |
{
|
1089 |
1089 |
typename Attributes::iterator it = _attributes.lower_bound(attr);
|
1090 |
1090 |
while (it != _attributes.end() && it->first == attr) {
|
1091 |
1091 |
it->second->set(token);
|
1092 |
1092 |
++it;
|
1093 |
1093 |
}
|
1094 |
1094 |
}
|
1095 |
1095 |
|
1096 |
1096 |
}
|
1097 |
1097 |
if (readSuccess()) {
|
1098 |
1098 |
line.putback(c);
|
1099 |
1099 |
}
|
1100 |
1100 |
for (typename Attributes::iterator it = _attributes.begin();
|
1101 |
1101 |
it != _attributes.end(); ++it) {
|
1102 |
1102 |
if (read_attr.find(it->first) == read_attr.end()) {
|
1103 |
1103 |
std::ostringstream msg;
|
1104 |
|
msg << "Attribute not found in file: " << it->first;
|
|
1104 |
msg << "Attribute not found: " << it->first;
|
1105 |
1105 |
throw FormatError(msg.str());
|
1106 |
1106 |
}
|
1107 |
1107 |
}
|
1108 |
1108 |
}
|
1109 |
1109 |
|
1110 |
1110 |
public:
|
1111 |
1111 |
|
1112 |
1112 |
/// \name Execution of the reader
|
1113 |
1113 |
/// @{
|
1114 |
1114 |
|
1115 |
1115 |
/// \brief Start the batch processing
|
1116 |
1116 |
///
|
1117 |
1117 |
/// This function starts the batch processing
|
1118 |
1118 |
void run() {
|
1119 |
1119 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
1120 |
|
if (!*_is) {
|
1121 |
|
throw FormatError("Cannot find file");
|
1122 |
|
}
|
1123 |
1120 |
|
1124 |
1121 |
bool nodes_done = _skip_nodes;
|
1125 |
1122 |
bool arcs_done = _skip_arcs;
|
1126 |
1123 |
bool attributes_done = false;
|
1127 |
1124 |
|
1128 |
1125 |
line_num = 0;
|
1129 |
1126 |
readLine();
|
1130 |
1127 |
skipSection();
|
1131 |
1128 |
|
1132 |
1129 |
while (readSuccess()) {
|
1133 |
1130 |
try {
|
1134 |
1131 |
char c;
|
1135 |
1132 |
std::string section, caption;
|
1136 |
1133 |
line >> c;
|
1137 |
1134 |
_reader_bits::readToken(line, section);
|
1138 |
1135 |
_reader_bits::readToken(line, caption);
|
1139 |
1136 |
|
1140 |
1137 |
if (line >> c)
|
1141 |
|
throw FormatError("Extra character on the end of line");
|
|
1138 |
throw FormatError("Extra character at the end of line");
|
1142 |
1139 |
|
1143 |
1140 |
if (section == "nodes" && !nodes_done) {
|
1144 |
1141 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
1145 |
1142 |
readNodes();
|
1146 |
1143 |
nodes_done = true;
|
1147 |
1144 |
}
|
1148 |
1145 |
} else if ((section == "arcs" || section == "edges") &&
|
1149 |
1146 |
!arcs_done) {
|
1150 |
1147 |
if (_arcs_caption.empty() || _arcs_caption == caption) {
|
1151 |
1148 |
readArcs();
|
1152 |
1149 |
arcs_done = true;
|
1153 |
1150 |
}
|
1154 |
1151 |
} else if (section == "attributes" && !attributes_done) {
|
1155 |
1152 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
1156 |
1153 |
readAttributes();
|
1157 |
1154 |
attributes_done = true;
|
1158 |
1155 |
}
|
1159 |
1156 |
} else {
|
1160 |
1157 |
readLine();
|
1161 |
1158 |
skipSection();
|
1162 |
1159 |
}
|
1163 |
1160 |
} catch (FormatError& error) {
|
1164 |
1161 |
error.line(line_num);
|
1165 |
1162 |
error.file(_filename);
|
1166 |
1163 |
throw;
|
1167 |
1164 |
}
|
1168 |
1165 |
}
|
1169 |
1166 |
|
1170 |
1167 |
if (!nodes_done) {
|
1171 |
1168 |
throw FormatError("Section @nodes not found");
|
1172 |
1169 |
}
|
1173 |
1170 |
|
1174 |
1171 |
if (!arcs_done) {
|
1175 |
1172 |
throw FormatError("Section @arcs not found");
|
1176 |
1173 |
}
|
1177 |
1174 |
|
1178 |
1175 |
if (!attributes_done && !_attributes.empty()) {
|
1179 |
1176 |
throw FormatError("Section @attributes not found");
|
1180 |
1177 |
}
|
1181 |
1178 |
|
1182 |
1179 |
}
|
1183 |
1180 |
|
1184 |
1181 |
/// @}
|
1185 |
1182 |
|
1186 |
1183 |
};
|
1187 |
1184 |
|
1188 |
1185 |
/// \brief Return a \ref DigraphReader class
|
1189 |
1186 |
///
|
1190 |
1187 |
/// This function just returns a \ref DigraphReader class.
|
1191 |
1188 |
/// \relates DigraphReader
|
1192 |
1189 |
template <typename Digraph>
|
1193 |
1190 |
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
|
1194 |
1191 |
DigraphReader<Digraph> tmp(is, digraph);
|
1195 |
1192 |
return tmp;
|
1196 |
1193 |
}
|
1197 |
1194 |
|
1198 |
1195 |
/// \brief Return a \ref DigraphReader class
|
1199 |
1196 |
///
|
1200 |
1197 |
/// This function just returns a \ref DigraphReader class.
|
1201 |
1198 |
/// \relates DigraphReader
|
1202 |
1199 |
template <typename Digraph>
|
1203 |
1200 |
DigraphReader<Digraph> digraphReader(const std::string& fn,
|
1204 |
1201 |
Digraph& digraph) {
|
1205 |
1202 |
DigraphReader<Digraph> tmp(fn, digraph);
|
1206 |
1203 |
return tmp;
|
1207 |
1204 |
}
|
1208 |
1205 |
|
1209 |
1206 |
/// \brief Return a \ref DigraphReader class
|
1210 |
1207 |
///
|
1211 |
1208 |
/// This function just returns a \ref DigraphReader class.
|
1212 |
1209 |
/// \relates DigraphReader
|
1213 |
1210 |
template <typename Digraph>
|
1214 |
1211 |
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
|
1215 |
1212 |
DigraphReader<Digraph> tmp(fn, digraph);
|
1216 |
1213 |
return tmp;
|
1217 |
1214 |
}
|
1218 |
1215 |
|
1219 |
1216 |
template <typename Graph>
|
1220 |
1217 |
class GraphReader;
|
1221 |
1218 |
|
1222 |
1219 |
template <typename Graph>
|
1223 |
1220 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph);
|
1224 |
1221 |
|
1225 |
1222 |
template <typename Graph>
|
1226 |
1223 |
GraphReader<Graph> graphReader(const std::string& fn, Graph& graph);
|
1227 |
1224 |
|
1228 |
1225 |
template <typename Graph>
|
1229 |
1226 |
GraphReader<Graph> graphReader(const char *fn, Graph& graph);
|
1230 |
1227 |
|
1231 |
1228 |
/// \ingroup lemon_io
|
1232 |
1229 |
///
|
1233 |
1230 |
/// \brief \ref lgf-format "LGF" reader for undirected graphs
|
1234 |
1231 |
///
|
1235 |
1232 |
/// This utility reads an \ref lgf-format "LGF" file.
|
1236 |
1233 |
///
|
1237 |
1234 |
/// It can be used almost the same way as \c DigraphReader.
|
1238 |
1235 |
/// The only difference is that this class can handle edges and
|
1239 |
1236 |
/// edge maps as well as arcs and arc maps.
|
1240 |
1237 |
///
|
1241 |
1238 |
/// The columns in the \c \@edges (or \c \@arcs) section are the
|
1242 |
1239 |
/// edge maps. However, if there are two maps with the same name
|
1243 |
1240 |
/// prefixed with \c '+' and \c '-', then these can be read into an
|
1244 |
1241 |
/// arc map. Similarly, an attribute can be read into an arc, if
|
1245 |
1242 |
/// it's value is an edge label prefixed with \c '+' or \c '-'.
|
1246 |
1243 |
template <typename _Graph>
|
1247 |
1244 |
class GraphReader {
|
1248 |
1245 |
public:
|
1249 |
1246 |
|
1250 |
1247 |
typedef _Graph Graph;
|
1251 |
1248 |
TEMPLATE_GRAPH_TYPEDEFS(Graph);
|
1252 |
1249 |
|
1253 |
1250 |
private:
|
1254 |
1251 |
|
1255 |
1252 |
std::istream* _is;
|
1256 |
1253 |
bool local_is;
|
1257 |
1254 |
std::string _filename;
|
1258 |
1255 |
|
1259 |
1256 |
Graph& _graph;
|
1260 |
1257 |
|
1261 |
1258 |
std::string _nodes_caption;
|
1262 |
1259 |
std::string _edges_caption;
|
1263 |
1260 |
std::string _attributes_caption;
|
1264 |
1261 |
|
1265 |
1262 |
typedef std::map<std::string, Node> NodeIndex;
|
1266 |
1263 |
NodeIndex _node_index;
|
1267 |
1264 |
typedef std::map<std::string, Edge> EdgeIndex;
|
1268 |
1265 |
EdgeIndex _edge_index;
|
1269 |
1266 |
|
1270 |
1267 |
typedef std::vector<std::pair<std::string,
|
1271 |
1268 |
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
|
1272 |
1269 |
NodeMaps _node_maps;
|
1273 |
1270 |
|
1274 |
1271 |
typedef std::vector<std::pair<std::string,
|
1275 |
1272 |
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
|
1276 |
1273 |
EdgeMaps _edge_maps;
|
1277 |
1274 |
|
1278 |
1275 |
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
|
1279 |
1276 |
Attributes;
|
1280 |
1277 |
Attributes _attributes;
|
1281 |
1278 |
|
1282 |
1279 |
bool _use_nodes;
|
1283 |
1280 |
bool _use_edges;
|
1284 |
1281 |
|
1285 |
1282 |
bool _skip_nodes;
|
1286 |
1283 |
bool _skip_edges;
|
1287 |
1284 |
|
1288 |
1285 |
int line_num;
|
1289 |
1286 |
std::istringstream line;
|
1290 |
1287 |
|
1291 |
1288 |
public:
|
1292 |
1289 |
|
1293 |
1290 |
/// \brief Constructor
|
1294 |
1291 |
///
|
1295 |
1292 |
/// Construct an undirected graph reader, which reads from the given
|
1296 |
1293 |
/// input stream.
|
1297 |
1294 |
GraphReader(std::istream& is, Graph& graph)
|
1298 |
1295 |
: _is(&is), local_is(false), _graph(graph),
|
1299 |
1296 |
_use_nodes(false), _use_edges(false),
|
1300 |
1297 |
_skip_nodes(false), _skip_edges(false) {}
|
1301 |
1298 |
|
1302 |
1299 |
/// \brief Constructor
|
1303 |
1300 |
///
|
1304 |
1301 |
/// Construct an undirected graph reader, which reads from the given
|
1305 |
1302 |
/// file.
|
1306 |
1303 |
GraphReader(const std::string& fn, Graph& graph)
|
1307 |
1304 |
: _is(new std::ifstream(fn.c_str())), local_is(true),
|
1308 |
1305 |
_filename(fn), _graph(graph),
|
1309 |
1306 |
_use_nodes(false), _use_edges(false),
|
1310 |
1307 |
_skip_nodes(false), _skip_edges(false) {
|
1311 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
1308 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
1312 |
1309 |
}
|
1313 |
1310 |
|
1314 |
1311 |
/// \brief Constructor
|
1315 |
1312 |
///
|
1316 |
1313 |
/// Construct an undirected graph reader, which reads from the given
|
1317 |
1314 |
/// file.
|
1318 |
1315 |
GraphReader(const char* fn, Graph& graph)
|
1319 |
1316 |
: _is(new std::ifstream(fn)), local_is(true),
|
1320 |
1317 |
_filename(fn), _graph(graph),
|
1321 |
1318 |
_use_nodes(false), _use_edges(false),
|
1322 |
1319 |
_skip_nodes(false), _skip_edges(false) {
|
1323 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
1320 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
1324 |
1321 |
}
|
1325 |
1322 |
|
1326 |
1323 |
/// \brief Destructor
|
1327 |
1324 |
~GraphReader() {
|
1328 |
1325 |
for (typename NodeMaps::iterator it = _node_maps.begin();
|
1329 |
1326 |
it != _node_maps.end(); ++it) {
|
1330 |
1327 |
delete it->second;
|
1331 |
1328 |
}
|
1332 |
1329 |
|
1333 |
1330 |
for (typename EdgeMaps::iterator it = _edge_maps.begin();
|
1334 |
1331 |
it != _edge_maps.end(); ++it) {
|
1335 |
1332 |
delete it->second;
|
1336 |
1333 |
}
|
1337 |
1334 |
|
1338 |
1335 |
for (typename Attributes::iterator it = _attributes.begin();
|
1339 |
1336 |
it != _attributes.end(); ++it) {
|
1340 |
1337 |
delete it->second;
|
1341 |
1338 |
}
|
1342 |
1339 |
|
1343 |
1340 |
if (local_is) {
|
1344 |
1341 |
delete _is;
|
1345 |
1342 |
}
|
1346 |
1343 |
|
1347 |
1344 |
}
|
1348 |
1345 |
|
1349 |
1346 |
private:
|
1350 |
1347 |
friend GraphReader<Graph> graphReader<>(std::istream& is, Graph& graph);
|
1351 |
1348 |
friend GraphReader<Graph> graphReader<>(const std::string& fn,
|
1352 |
1349 |
Graph& graph);
|
1353 |
1350 |
friend GraphReader<Graph> graphReader<>(const char *fn, Graph& graph);
|
1354 |
1351 |
|
1355 |
1352 |
GraphReader(GraphReader& other)
|
1356 |
1353 |
: _is(other._is), local_is(other.local_is), _graph(other._graph),
|
1357 |
1354 |
_use_nodes(other._use_nodes), _use_edges(other._use_edges),
|
1358 |
1355 |
_skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
|
1359 |
1356 |
|
1360 |
1357 |
other._is = 0;
|
1361 |
1358 |
other.local_is = false;
|
1362 |
1359 |
|
1363 |
1360 |
_node_index.swap(other._node_index);
|
1364 |
1361 |
_edge_index.swap(other._edge_index);
|
1365 |
1362 |
|
1366 |
1363 |
_node_maps.swap(other._node_maps);
|
1367 |
1364 |
_edge_maps.swap(other._edge_maps);
|
1368 |
1365 |
_attributes.swap(other._attributes);
|
1369 |
1366 |
|
1370 |
1367 |
_nodes_caption = other._nodes_caption;
|
1371 |
1368 |
_edges_caption = other._edges_caption;
|
1372 |
1369 |
_attributes_caption = other._attributes_caption;
|
1373 |
1370 |
|
1374 |
1371 |
}
|
1375 |
1372 |
|
1376 |
1373 |
GraphReader& operator=(const GraphReader&);
|
1377 |
1374 |
|
1378 |
1375 |
public:
|
1379 |
1376 |
|
1380 |
1377 |
/// \name Reading rules
|
1381 |
1378 |
/// @{
|
1382 |
1379 |
|
1383 |
1380 |
/// \brief Node map reading rule
|
1384 |
1381 |
///
|
1385 |
1382 |
/// Add a node map reading rule to the reader.
|
1386 |
1383 |
template <typename Map>
|
1387 |
1384 |
GraphReader& nodeMap(const std::string& caption, Map& map) {
|
1388 |
1385 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
1389 |
1386 |
_reader_bits::MapStorageBase<Node>* storage =
|
1390 |
1387 |
new _reader_bits::MapStorage<Node, Map>(map);
|
1391 |
1388 |
_node_maps.push_back(std::make_pair(caption, storage));
|
1392 |
1389 |
return *this;
|
1393 |
1390 |
}
|
1394 |
1391 |
|
1395 |
1392 |
/// \brief Node map reading rule
|
1396 |
1393 |
///
|
1397 |
1394 |
/// Add a node map reading rule with specialized converter to the
|
1398 |
1395 |
/// reader.
|
1399 |
1396 |
template <typename Map, typename Converter>
|
1400 |
1397 |
GraphReader& nodeMap(const std::string& caption, Map& map,
|
1401 |
1398 |
const Converter& converter = Converter()) {
|
1402 |
1399 |
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
|
1403 |
1400 |
_reader_bits::MapStorageBase<Node>* storage =
|
1404 |
1401 |
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
|
1405 |
1402 |
_node_maps.push_back(std::make_pair(caption, storage));
|
1406 |
1403 |
return *this;
|
1407 |
1404 |
}
|
1408 |
1405 |
|
1409 |
1406 |
/// \brief Edge map reading rule
|
1410 |
1407 |
///
|
1411 |
1408 |
/// Add an edge map reading rule to the reader.
|
1412 |
1409 |
template <typename Map>
|
1413 |
1410 |
GraphReader& edgeMap(const std::string& caption, Map& map) {
|
1414 |
1411 |
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
|
1415 |
1412 |
_reader_bits::MapStorageBase<Edge>* storage =
|
1416 |
1413 |
new _reader_bits::MapStorage<Edge, Map>(map);
|
1417 |
1414 |
_edge_maps.push_back(std::make_pair(caption, storage));
|
1418 |
1415 |
return *this;
|
1419 |
1416 |
}
|
... |
... |
@@ -1622,874 +1619,874 @@
|
1622 |
1619 |
_use_edges = true;
|
1623 |
1620 |
for (EdgeIt a(_graph); a != INVALID; ++a) {
|
1624 |
1621 |
_edge_index.insert(std::make_pair(converter(map[a]), a));
|
1625 |
1622 |
}
|
1626 |
1623 |
return *this;
|
1627 |
1624 |
}
|
1628 |
1625 |
|
1629 |
1626 |
/// \brief Skip the reading of node section
|
1630 |
1627 |
///
|
1631 |
1628 |
/// Omit the reading of the node section. This implies that each node
|
1632 |
1629 |
/// map reading rule will be abandoned, and the nodes of the graph
|
1633 |
1630 |
/// will not be constructed, which usually cause that the edge set
|
1634 |
1631 |
/// could not be read due to lack of node name
|
1635 |
1632 |
/// could not be read due to lack of node name resolving.
|
1636 |
1633 |
/// Therefore \c skipEdges() function should also be used, or
|
1637 |
1634 |
/// \c useNodes() should be used to specify the label of the nodes.
|
1638 |
1635 |
GraphReader& skipNodes() {
|
1639 |
1636 |
LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
|
1640 |
1637 |
_skip_nodes = true;
|
1641 |
1638 |
return *this;
|
1642 |
1639 |
}
|
1643 |
1640 |
|
1644 |
1641 |
/// \brief Skip the reading of edge section
|
1645 |
1642 |
///
|
1646 |
1643 |
/// Omit the reading of the edge section. This implies that each edge
|
1647 |
1644 |
/// map reading rule will be abandoned, and the edges of the graph
|
1648 |
1645 |
/// will not be constructed.
|
1649 |
1646 |
GraphReader& skipEdges() {
|
1650 |
1647 |
LEMON_ASSERT(!_skip_edges, "Skip edges already set");
|
1651 |
1648 |
_skip_edges = true;
|
1652 |
1649 |
return *this;
|
1653 |
1650 |
}
|
1654 |
1651 |
|
1655 |
1652 |
/// @}
|
1656 |
1653 |
|
1657 |
1654 |
private:
|
1658 |
1655 |
|
1659 |
1656 |
bool readLine() {
|
1660 |
1657 |
std::string str;
|
1661 |
1658 |
while(++line_num, std::getline(*_is, str)) {
|
1662 |
1659 |
line.clear(); line.str(str);
|
1663 |
1660 |
char c;
|
1664 |
1661 |
if (line >> std::ws >> c && c != '#') {
|
1665 |
1662 |
line.putback(c);
|
1666 |
1663 |
return true;
|
1667 |
1664 |
}
|
1668 |
1665 |
}
|
1669 |
1666 |
return false;
|
1670 |
1667 |
}
|
1671 |
1668 |
|
1672 |
1669 |
bool readSuccess() {
|
1673 |
1670 |
return static_cast<bool>(*_is);
|
1674 |
1671 |
}
|
1675 |
1672 |
|
1676 |
1673 |
void skipSection() {
|
1677 |
1674 |
char c;
|
1678 |
1675 |
while (readSuccess() && line >> c && c != '@') {
|
1679 |
1676 |
readLine();
|
1680 |
1677 |
}
|
1681 |
1678 |
line.putback(c);
|
1682 |
1679 |
}
|
1683 |
1680 |
|
1684 |
1681 |
void readNodes() {
|
1685 |
1682 |
|
1686 |
1683 |
std::vector<int> map_index(_node_maps.size());
|
1687 |
1684 |
int map_num, label_index;
|
1688 |
1685 |
|
1689 |
1686 |
char c;
|
1690 |
1687 |
if (!readLine() || !(line >> c) || c == '@') {
|
1691 |
1688 |
if (readSuccess() && line) line.putback(c);
|
1692 |
1689 |
if (!_node_maps.empty())
|
1693 |
1690 |
throw FormatError("Cannot find map names");
|
1694 |
1691 |
return;
|
1695 |
1692 |
}
|
1696 |
1693 |
line.putback(c);
|
1697 |
1694 |
|
1698 |
1695 |
{
|
1699 |
1696 |
std::map<std::string, int> maps;
|
1700 |
1697 |
|
1701 |
1698 |
std::string map;
|
1702 |
1699 |
int index = 0;
|
1703 |
1700 |
while (_reader_bits::readToken(line, map)) {
|
1704 |
1701 |
if (maps.find(map) != maps.end()) {
|
1705 |
1702 |
std::ostringstream msg;
|
1706 |
1703 |
msg << "Multiple occurence of node map: " << map;
|
1707 |
1704 |
throw FormatError(msg.str());
|
1708 |
1705 |
}
|
1709 |
1706 |
maps.insert(std::make_pair(map, index));
|
1710 |
1707 |
++index;
|
1711 |
1708 |
}
|
1712 |
1709 |
|
1713 |
1710 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
1714 |
1711 |
std::map<std::string, int>::iterator jt =
|
1715 |
1712 |
maps.find(_node_maps[i].first);
|
1716 |
1713 |
if (jt == maps.end()) {
|
1717 |
1714 |
std::ostringstream msg;
|
1718 |
|
msg << "Map not found in file: " << _node_maps[i].first;
|
|
1715 |
msg << "Map not found: " << _node_maps[i].first;
|
1719 |
1716 |
throw FormatError(msg.str());
|
1720 |
1717 |
}
|
1721 |
1718 |
map_index[i] = jt->second;
|
1722 |
1719 |
}
|
1723 |
1720 |
|
1724 |
1721 |
{
|
1725 |
1722 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
1726 |
1723 |
if (jt != maps.end()) {
|
1727 |
1724 |
label_index = jt->second;
|
1728 |
1725 |
} else {
|
1729 |
1726 |
label_index = -1;
|
1730 |
1727 |
}
|
1731 |
1728 |
}
|
1732 |
1729 |
map_num = maps.size();
|
1733 |
1730 |
}
|
1734 |
1731 |
|
1735 |
1732 |
while (readLine() && line >> c && c != '@') {
|
1736 |
1733 |
line.putback(c);
|
1737 |
1734 |
|
1738 |
1735 |
std::vector<std::string> tokens(map_num);
|
1739 |
1736 |
for (int i = 0; i < map_num; ++i) {
|
1740 |
1737 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
1741 |
1738 |
std::ostringstream msg;
|
1742 |
1739 |
msg << "Column not found (" << i + 1 << ")";
|
1743 |
1740 |
throw FormatError(msg.str());
|
1744 |
1741 |
}
|
1745 |
1742 |
}
|
1746 |
1743 |
if (line >> std::ws >> c)
|
1747 |
|
throw FormatError("Extra character on the end of line");
|
|
1744 |
throw FormatError("Extra character at the end of line");
|
1748 |
1745 |
|
1749 |
1746 |
Node n;
|
1750 |
1747 |
if (!_use_nodes) {
|
1751 |
1748 |
n = _graph.addNode();
|
1752 |
1749 |
if (label_index != -1)
|
1753 |
1750 |
_node_index.insert(std::make_pair(tokens[label_index], n));
|
1754 |
1751 |
} else {
|
1755 |
1752 |
if (label_index == -1)
|
1756 |
|
throw FormatError("Label map not found in file");
|
|
1753 |
throw FormatError("Label map not found");
|
1757 |
1754 |
typename std::map<std::string, Node>::iterator it =
|
1758 |
1755 |
_node_index.find(tokens[label_index]);
|
1759 |
1756 |
if (it == _node_index.end()) {
|
1760 |
1757 |
std::ostringstream msg;
|
1761 |
1758 |
msg << "Node with label not found: " << tokens[label_index];
|
1762 |
1759 |
throw FormatError(msg.str());
|
1763 |
1760 |
}
|
1764 |
1761 |
n = it->second;
|
1765 |
1762 |
}
|
1766 |
1763 |
|
1767 |
1764 |
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
|
1768 |
1765 |
_node_maps[i].second->set(n, tokens[map_index[i]]);
|
1769 |
1766 |
}
|
1770 |
1767 |
|
1771 |
1768 |
}
|
1772 |
1769 |
if (readSuccess()) {
|
1773 |
1770 |
line.putback(c);
|
1774 |
1771 |
}
|
1775 |
1772 |
}
|
1776 |
1773 |
|
1777 |
1774 |
void readEdges() {
|
1778 |
1775 |
|
1779 |
1776 |
std::vector<int> map_index(_edge_maps.size());
|
1780 |
1777 |
int map_num, label_index;
|
1781 |
1778 |
|
1782 |
1779 |
char c;
|
1783 |
1780 |
if (!readLine() || !(line >> c) || c == '@') {
|
1784 |
1781 |
if (readSuccess() && line) line.putback(c);
|
1785 |
1782 |
if (!_edge_maps.empty())
|
1786 |
1783 |
throw FormatError("Cannot find map names");
|
1787 |
1784 |
return;
|
1788 |
1785 |
}
|
1789 |
1786 |
line.putback(c);
|
1790 |
1787 |
|
1791 |
1788 |
{
|
1792 |
1789 |
std::map<std::string, int> maps;
|
1793 |
1790 |
|
1794 |
1791 |
std::string map;
|
1795 |
1792 |
int index = 0;
|
1796 |
1793 |
while (_reader_bits::readToken(line, map)) {
|
1797 |
1794 |
if (maps.find(map) != maps.end()) {
|
1798 |
1795 |
std::ostringstream msg;
|
1799 |
1796 |
msg << "Multiple occurence of edge map: " << map;
|
1800 |
1797 |
throw FormatError(msg.str());
|
1801 |
1798 |
}
|
1802 |
1799 |
maps.insert(std::make_pair(map, index));
|
1803 |
1800 |
++index;
|
1804 |
1801 |
}
|
1805 |
1802 |
|
1806 |
1803 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
1807 |
1804 |
std::map<std::string, int>::iterator jt =
|
1808 |
1805 |
maps.find(_edge_maps[i].first);
|
1809 |
1806 |
if (jt == maps.end()) {
|
1810 |
1807 |
std::ostringstream msg;
|
1811 |
|
msg << "Map not found in file: " << _edge_maps[i].first;
|
|
1808 |
msg << "Map not found: " << _edge_maps[i].first;
|
1812 |
1809 |
throw FormatError(msg.str());
|
1813 |
1810 |
}
|
1814 |
1811 |
map_index[i] = jt->second;
|
1815 |
1812 |
}
|
1816 |
1813 |
|
1817 |
1814 |
{
|
1818 |
1815 |
std::map<std::string, int>::iterator jt = maps.find("label");
|
1819 |
1816 |
if (jt != maps.end()) {
|
1820 |
1817 |
label_index = jt->second;
|
1821 |
1818 |
} else {
|
1822 |
1819 |
label_index = -1;
|
1823 |
1820 |
}
|
1824 |
1821 |
}
|
1825 |
1822 |
map_num = maps.size();
|
1826 |
1823 |
}
|
1827 |
1824 |
|
1828 |
1825 |
while (readLine() && line >> c && c != '@') {
|
1829 |
1826 |
line.putback(c);
|
1830 |
1827 |
|
1831 |
1828 |
std::string source_token;
|
1832 |
1829 |
std::string target_token;
|
1833 |
1830 |
|
1834 |
1831 |
if (!_reader_bits::readToken(line, source_token))
|
1835 |
1832 |
throw FormatError("Node u not found");
|
1836 |
1833 |
|
1837 |
1834 |
if (!_reader_bits::readToken(line, target_token))
|
1838 |
1835 |
throw FormatError("Node v not found");
|
1839 |
1836 |
|
1840 |
1837 |
std::vector<std::string> tokens(map_num);
|
1841 |
1838 |
for (int i = 0; i < map_num; ++i) {
|
1842 |
1839 |
if (!_reader_bits::readToken(line, tokens[i])) {
|
1843 |
1840 |
std::ostringstream msg;
|
1844 |
1841 |
msg << "Column not found (" << i + 1 << ")";
|
1845 |
1842 |
throw FormatError(msg.str());
|
1846 |
1843 |
}
|
1847 |
1844 |
}
|
1848 |
1845 |
if (line >> std::ws >> c)
|
1849 |
|
throw FormatError("Extra character on the end of line");
|
|
1846 |
throw FormatError("Extra character at the end of line");
|
1850 |
1847 |
|
1851 |
1848 |
Edge e;
|
1852 |
1849 |
if (!_use_edges) {
|
1853 |
1850 |
|
1854 |
1851 |
typename NodeIndex::iterator it;
|
1855 |
1852 |
|
1856 |
1853 |
it = _node_index.find(source_token);
|
1857 |
1854 |
if (it == _node_index.end()) {
|
1858 |
1855 |
std::ostringstream msg;
|
1859 |
1856 |
msg << "Item not found: " << source_token;
|
1860 |
1857 |
throw FormatError(msg.str());
|
1861 |
1858 |
}
|
1862 |
1859 |
Node source = it->second;
|
1863 |
1860 |
|
1864 |
1861 |
it = _node_index.find(target_token);
|
1865 |
1862 |
if (it == _node_index.end()) {
|
1866 |
1863 |
std::ostringstream msg;
|
1867 |
1864 |
msg << "Item not found: " << target_token;
|
1868 |
1865 |
throw FormatError(msg.str());
|
1869 |
1866 |
}
|
1870 |
1867 |
Node target = it->second;
|
1871 |
1868 |
|
1872 |
1869 |
e = _graph.addEdge(source, target);
|
1873 |
1870 |
if (label_index != -1)
|
1874 |
1871 |
_edge_index.insert(std::make_pair(tokens[label_index], e));
|
1875 |
1872 |
} else {
|
1876 |
1873 |
if (label_index == -1)
|
1877 |
|
throw FormatError("Label map not found in file");
|
|
1874 |
throw FormatError("Label map not found");
|
1878 |
1875 |
typename std::map<std::string, Edge>::iterator it =
|
1879 |
1876 |
_edge_index.find(tokens[label_index]);
|
1880 |
1877 |
if (it == _edge_index.end()) {
|
1881 |
1878 |
std::ostringstream msg;
|
1882 |
1879 |
msg << "Edge with label not found: " << tokens[label_index];
|
1883 |
1880 |
throw FormatError(msg.str());
|
1884 |
1881 |
}
|
1885 |
1882 |
e = it->second;
|
1886 |
1883 |
}
|
1887 |
1884 |
|
1888 |
1885 |
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
|
1889 |
1886 |
_edge_maps[i].second->set(e, tokens[map_index[i]]);
|
1890 |
1887 |
}
|
1891 |
1888 |
|
1892 |
1889 |
}
|
1893 |
1890 |
if (readSuccess()) {
|
1894 |
1891 |
line.putback(c);
|
1895 |
1892 |
}
|
1896 |
1893 |
}
|
1897 |
1894 |
|
1898 |
1895 |
void readAttributes() {
|
1899 |
1896 |
|
1900 |
1897 |
std::set<std::string> read_attr;
|
1901 |
1898 |
|
1902 |
1899 |
char c;
|
1903 |
1900 |
while (readLine() && line >> c && c != '@') {
|
1904 |
1901 |
line.putback(c);
|
1905 |
1902 |
|
1906 |
1903 |
std::string attr, token;
|
1907 |
1904 |
if (!_reader_bits::readToken(line, attr))
|
1908 |
1905 |
throw FormatError("Attribute name not found");
|
1909 |
1906 |
if (!_reader_bits::readToken(line, token))
|
1910 |
1907 |
throw FormatError("Attribute value not found");
|
1911 |
1908 |
if (line >> c)
|
1912 |
|
throw FormatError("Extra character on the end of line");
|
|
1909 |
throw FormatError("Extra character at the end of line");
|
1913 |
1910 |
|
1914 |
1911 |
{
|
1915 |
1912 |
std::set<std::string>::iterator it = read_attr.find(attr);
|
1916 |
1913 |
if (it != read_attr.end()) {
|
1917 |
1914 |
std::ostringstream msg;
|
1918 |
|
msg << "Multiple occurence of attribute " << attr;
|
|
1915 |
msg << "Multiple occurence of attribute: " << attr;
|
1919 |
1916 |
throw FormatError(msg.str());
|
1920 |
1917 |
}
|
1921 |
1918 |
read_attr.insert(attr);
|
1922 |
1919 |
}
|
1923 |
1920 |
|
1924 |
1921 |
{
|
1925 |
1922 |
typename Attributes::iterator it = _attributes.lower_bound(attr);
|
1926 |
1923 |
while (it != _attributes.end() && it->first == attr) {
|
1927 |
1924 |
it->second->set(token);
|
1928 |
1925 |
++it;
|
1929 |
1926 |
}
|
1930 |
1927 |
}
|
1931 |
1928 |
|
1932 |
1929 |
}
|
1933 |
1930 |
if (readSuccess()) {
|
1934 |
1931 |
line.putback(c);
|
1935 |
1932 |
}
|
1936 |
1933 |
for (typename Attributes::iterator it = _attributes.begin();
|
1937 |
1934 |
it != _attributes.end(); ++it) {
|
1938 |
1935 |
if (read_attr.find(it->first) == read_attr.end()) {
|
1939 |
1936 |
std::ostringstream msg;
|
1940 |
|
msg << "Attribute not found in file: " << it->first;
|
|
1937 |
msg << "Attribute not found: " << it->first;
|
1941 |
1938 |
throw FormatError(msg.str());
|
1942 |
1939 |
}
|
1943 |
1940 |
}
|
1944 |
1941 |
}
|
1945 |
1942 |
|
1946 |
1943 |
public:
|
1947 |
1944 |
|
1948 |
1945 |
/// \name Execution of the reader
|
1949 |
1946 |
/// @{
|
1950 |
1947 |
|
1951 |
1948 |
/// \brief Start the batch processing
|
1952 |
1949 |
///
|
1953 |
1950 |
/// This function starts the batch processing
|
1954 |
1951 |
void run() {
|
1955 |
1952 |
|
1956 |
1953 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
1957 |
1954 |
|
1958 |
1955 |
bool nodes_done = _skip_nodes;
|
1959 |
1956 |
bool edges_done = _skip_edges;
|
1960 |
1957 |
bool attributes_done = false;
|
1961 |
1958 |
|
1962 |
1959 |
line_num = 0;
|
1963 |
1960 |
readLine();
|
1964 |
1961 |
skipSection();
|
1965 |
1962 |
|
1966 |
1963 |
while (readSuccess()) {
|
1967 |
1964 |
try {
|
1968 |
1965 |
char c;
|
1969 |
1966 |
std::string section, caption;
|
1970 |
1967 |
line >> c;
|
1971 |
1968 |
_reader_bits::readToken(line, section);
|
1972 |
1969 |
_reader_bits::readToken(line, caption);
|
1973 |
1970 |
|
1974 |
1971 |
if (line >> c)
|
1975 |
|
throw FormatError("Extra character on the end of line");
|
|
1972 |
throw FormatError("Extra character at the end of line");
|
1976 |
1973 |
|
1977 |
1974 |
if (section == "nodes" && !nodes_done) {
|
1978 |
1975 |
if (_nodes_caption.empty() || _nodes_caption == caption) {
|
1979 |
1976 |
readNodes();
|
1980 |
1977 |
nodes_done = true;
|
1981 |
1978 |
}
|
1982 |
1979 |
} else if ((section == "edges" || section == "arcs") &&
|
1983 |
1980 |
!edges_done) {
|
1984 |
1981 |
if (_edges_caption.empty() || _edges_caption == caption) {
|
1985 |
1982 |
readEdges();
|
1986 |
1983 |
edges_done = true;
|
1987 |
1984 |
}
|
1988 |
1985 |
} else if (section == "attributes" && !attributes_done) {
|
1989 |
1986 |
if (_attributes_caption.empty() || _attributes_caption == caption) {
|
1990 |
1987 |
readAttributes();
|
1991 |
1988 |
attributes_done = true;
|
1992 |
1989 |
}
|
1993 |
1990 |
} else {
|
1994 |
1991 |
readLine();
|
1995 |
1992 |
skipSection();
|
1996 |
1993 |
}
|
1997 |
1994 |
} catch (FormatError& error) {
|
1998 |
1995 |
error.line(line_num);
|
1999 |
1996 |
error.file(_filename);
|
2000 |
1997 |
throw;
|
2001 |
1998 |
}
|
2002 |
1999 |
}
|
2003 |
2000 |
|
2004 |
2001 |
if (!nodes_done) {
|
2005 |
2002 |
throw FormatError("Section @nodes not found");
|
2006 |
2003 |
}
|
2007 |
2004 |
|
2008 |
2005 |
if (!edges_done) {
|
2009 |
2006 |
throw FormatError("Section @edges not found");
|
2010 |
2007 |
}
|
2011 |
2008 |
|
2012 |
2009 |
if (!attributes_done && !_attributes.empty()) {
|
2013 |
2010 |
throw FormatError("Section @attributes not found");
|
2014 |
2011 |
}
|
2015 |
2012 |
|
2016 |
2013 |
}
|
2017 |
2014 |
|
2018 |
2015 |
/// @}
|
2019 |
2016 |
|
2020 |
2017 |
};
|
2021 |
2018 |
|
2022 |
2019 |
/// \brief Return a \ref GraphReader class
|
2023 |
2020 |
///
|
2024 |
2021 |
/// This function just returns a \ref GraphReader class.
|
2025 |
2022 |
/// \relates GraphReader
|
2026 |
2023 |
template <typename Graph>
|
2027 |
2024 |
GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
|
2028 |
2025 |
GraphReader<Graph> tmp(is, graph);
|
2029 |
2026 |
return tmp;
|
2030 |
2027 |
}
|
2031 |
2028 |
|
2032 |
2029 |
/// \brief Return a \ref GraphReader class
|
2033 |
2030 |
///
|
2034 |
2031 |
/// This function just returns a \ref GraphReader class.
|
2035 |
2032 |
/// \relates GraphReader
|
2036 |
2033 |
template <typename Graph>
|
2037 |
2034 |
GraphReader<Graph> graphReader(const std::string& fn,
|
2038 |
2035 |
Graph& graph) {
|
2039 |
2036 |
GraphReader<Graph> tmp(fn, graph);
|
2040 |
2037 |
return tmp;
|
2041 |
2038 |
}
|
2042 |
2039 |
|
2043 |
2040 |
/// \brief Return a \ref GraphReader class
|
2044 |
2041 |
///
|
2045 |
2042 |
/// This function just returns a \ref GraphReader class.
|
2046 |
2043 |
/// \relates GraphReader
|
2047 |
2044 |
template <typename Graph>
|
2048 |
2045 |
GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
|
2049 |
2046 |
GraphReader<Graph> tmp(fn, graph);
|
2050 |
2047 |
return tmp;
|
2051 |
2048 |
}
|
2052 |
2049 |
|
2053 |
2050 |
class SectionReader;
|
2054 |
2051 |
|
2055 |
2052 |
SectionReader sectionReader(std::istream& is);
|
2056 |
2053 |
SectionReader sectionReader(const std::string& fn);
|
2057 |
2054 |
SectionReader sectionReader(const char* fn);
|
2058 |
2055 |
|
2059 |
2056 |
/// \ingroup lemon_io
|
2060 |
2057 |
///
|
2061 |
2058 |
/// \brief Section reader class
|
2062 |
2059 |
///
|
2063 |
2060 |
/// In the \ref lgf-format "LGF" file extra sections can be placed,
|
2064 |
2061 |
/// which contain any data in arbitrary format. Such sections can be
|
2065 |
2062 |
/// read with this class. A reading rule can be added to the class
|
2066 |
2063 |
/// with two different functions. With the \c sectionLines() function a
|
2067 |
2064 |
/// functor can process the section line-by-line, while with the \c
|
2068 |
2065 |
/// sectionStream() member the section can be read from an input
|
2069 |
2066 |
/// stream.
|
2070 |
2067 |
class SectionReader {
|
2071 |
2068 |
private:
|
2072 |
2069 |
|
2073 |
2070 |
std::istream* _is;
|
2074 |
2071 |
bool local_is;
|
2075 |
2072 |
std::string _filename;
|
2076 |
2073 |
|
2077 |
2074 |
typedef std::map<std::string, _reader_bits::Section*> Sections;
|
2078 |
2075 |
Sections _sections;
|
2079 |
2076 |
|
2080 |
2077 |
int line_num;
|
2081 |
2078 |
std::istringstream line;
|
2082 |
2079 |
|
2083 |
2080 |
public:
|
2084 |
2081 |
|
2085 |
2082 |
/// \brief Constructor
|
2086 |
2083 |
///
|
2087 |
2084 |
/// Construct a section reader, which reads from the given input
|
2088 |
2085 |
/// stream.
|
2089 |
2086 |
SectionReader(std::istream& is)
|
2090 |
2087 |
: _is(&is), local_is(false) {}
|
2091 |
2088 |
|
2092 |
2089 |
/// \brief Constructor
|
2093 |
2090 |
///
|
2094 |
2091 |
/// Construct a section reader, which reads from the given file.
|
2095 |
2092 |
SectionReader(const std::string& fn)
|
2096 |
2093 |
: _is(new std::ifstream(fn.c_str())), local_is(true),
|
2097 |
2094 |
_filename(fn) {
|
2098 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
2095 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
2099 |
2096 |
}
|
2100 |
2097 |
|
2101 |
2098 |
/// \brief Constructor
|
2102 |
2099 |
///
|
2103 |
2100 |
/// Construct a section reader, which reads from the given file.
|
2104 |
2101 |
SectionReader(const char* fn)
|
2105 |
2102 |
: _is(new std::ifstream(fn)), local_is(true),
|
2106 |
2103 |
_filename(fn) {
|
2107 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
2104 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
2108 |
2105 |
}
|
2109 |
2106 |
|
2110 |
2107 |
/// \brief Destructor
|
2111 |
2108 |
~SectionReader() {
|
2112 |
2109 |
for (Sections::iterator it = _sections.begin();
|
2113 |
2110 |
it != _sections.end(); ++it) {
|
2114 |
2111 |
delete it->second;
|
2115 |
2112 |
}
|
2116 |
2113 |
|
2117 |
2114 |
if (local_is) {
|
2118 |
2115 |
delete _is;
|
2119 |
2116 |
}
|
2120 |
2117 |
|
2121 |
2118 |
}
|
2122 |
2119 |
|
2123 |
2120 |
private:
|
2124 |
2121 |
|
2125 |
2122 |
friend SectionReader sectionReader(std::istream& is);
|
2126 |
2123 |
friend SectionReader sectionReader(const std::string& fn);
|
2127 |
2124 |
friend SectionReader sectionReader(const char* fn);
|
2128 |
2125 |
|
2129 |
2126 |
SectionReader(SectionReader& other)
|
2130 |
2127 |
: _is(other._is), local_is(other.local_is) {
|
2131 |
2128 |
|
2132 |
2129 |
other._is = 0;
|
2133 |
2130 |
other.local_is = false;
|
2134 |
2131 |
|
2135 |
2132 |
_sections.swap(other._sections);
|
2136 |
2133 |
}
|
2137 |
2134 |
|
2138 |
2135 |
SectionReader& operator=(const SectionReader&);
|
2139 |
2136 |
|
2140 |
2137 |
public:
|
2141 |
2138 |
|
2142 |
2139 |
/// \name Section readers
|
2143 |
2140 |
/// @{
|
2144 |
2141 |
|
2145 |
2142 |
/// \brief Add a section processor with line oriented reading
|
2146 |
2143 |
///
|
2147 |
2144 |
/// The first parameter is the type descriptor of the section, the
|
2148 |
2145 |
/// second is a functor, which takes just one \c std::string
|
2149 |
2146 |
/// parameter. At the reading process, each line of the section
|
2150 |
2147 |
/// will be given to the functor object. However, the empty lines
|
2151 |
2148 |
/// and the comment lines are filtered out, and the leading
|
2152 |
2149 |
/// whitespaces are trimmed from each processed string.
|
2153 |
2150 |
///
|
2154 |
2151 |
/// For example let's see a section, which contain several
|
2155 |
2152 |
/// integers, which should be inserted into a vector.
|
2156 |
2153 |
///\code
|
2157 |
2154 |
/// @numbers
|
2158 |
2155 |
/// 12 45 23
|
2159 |
2156 |
/// 4
|
2160 |
2157 |
/// 23 6
|
2161 |
2158 |
///\endcode
|
2162 |
2159 |
///
|
2163 |
2160 |
/// The functor is implemented as a struct:
|
2164 |
2161 |
///\code
|
2165 |
2162 |
/// struct NumberSection {
|
2166 |
2163 |
/// std::vector<int>& _data;
|
2167 |
2164 |
/// NumberSection(std::vector<int>& data) : _data(data) {}
|
2168 |
2165 |
/// void operator()(const std::string& line) {
|
2169 |
2166 |
/// std::istringstream ls(line);
|
2170 |
2167 |
/// int value;
|
2171 |
2168 |
/// while (ls >> value) _data.push_back(value);
|
2172 |
2169 |
/// }
|
2173 |
2170 |
/// };
|
2174 |
2171 |
///
|
2175 |
2172 |
/// // ...
|
2176 |
2173 |
///
|
2177 |
2174 |
/// reader.sectionLines("numbers", NumberSection(vec));
|
2178 |
2175 |
///\endcode
|
2179 |
2176 |
template <typename Functor>
|
2180 |
2177 |
SectionReader& sectionLines(const std::string& type, Functor functor) {
|
2181 |
2178 |
LEMON_ASSERT(!type.empty(), "Type is empty.");
|
2182 |
2179 |
LEMON_ASSERT(_sections.find(type) == _sections.end(),
|
2183 |
2180 |
"Multiple reading of section.");
|
2184 |
2181 |
_sections.insert(std::make_pair(type,
|
2185 |
2182 |
new _reader_bits::LineSection<Functor>(functor)));
|
2186 |
2183 |
return *this;
|
2187 |
2184 |
}
|
2188 |
2185 |
|
2189 |
2186 |
|
2190 |
2187 |
/// \brief Add a section processor with stream oriented reading
|
2191 |
2188 |
///
|
2192 |
2189 |
/// The first parameter is the type of the section, the second is
|
2193 |
2190 |
/// a functor, which takes an \c std::istream& and an \c int&
|
2194 |
2191 |
/// parameter, the latter regard to the line number of stream. The
|
2195 |
2192 |
/// functor can read the input while the section go on, and the
|
2196 |
2193 |
/// line number should be modified accordingly.
|
2197 |
2194 |
template <typename Functor>
|
2198 |
2195 |
SectionReader& sectionStream(const std::string& type, Functor functor) {
|
2199 |
2196 |
LEMON_ASSERT(!type.empty(), "Type is empty.");
|
2200 |
2197 |
LEMON_ASSERT(_sections.find(type) == _sections.end(),
|
2201 |
2198 |
"Multiple reading of section.");
|
2202 |
2199 |
_sections.insert(std::make_pair(type,
|
2203 |
2200 |
new _reader_bits::StreamSection<Functor>(functor)));
|
2204 |
2201 |
return *this;
|
2205 |
2202 |
}
|
2206 |
2203 |
|
2207 |
2204 |
/// @}
|
2208 |
2205 |
|
2209 |
2206 |
private:
|
2210 |
2207 |
|
2211 |
2208 |
bool readLine() {
|
2212 |
2209 |
std::string str;
|
2213 |
2210 |
while(++line_num, std::getline(*_is, str)) {
|
2214 |
2211 |
line.clear(); line.str(str);
|
2215 |
2212 |
char c;
|
2216 |
2213 |
if (line >> std::ws >> c && c != '#') {
|
2217 |
2214 |
line.putback(c);
|
2218 |
2215 |
return true;
|
2219 |
2216 |
}
|
2220 |
2217 |
}
|
2221 |
2218 |
return false;
|
2222 |
2219 |
}
|
2223 |
2220 |
|
2224 |
2221 |
bool readSuccess() {
|
2225 |
2222 |
return static_cast<bool>(*_is);
|
2226 |
2223 |
}
|
2227 |
2224 |
|
2228 |
2225 |
void skipSection() {
|
2229 |
2226 |
char c;
|
2230 |
2227 |
while (readSuccess() && line >> c && c != '@') {
|
2231 |
2228 |
readLine();
|
2232 |
2229 |
}
|
2233 |
2230 |
line.putback(c);
|
2234 |
2231 |
}
|
2235 |
2232 |
|
2236 |
2233 |
public:
|
2237 |
2234 |
|
2238 |
2235 |
|
2239 |
2236 |
/// \name Execution of the reader
|
2240 |
2237 |
/// @{
|
2241 |
2238 |
|
2242 |
2239 |
/// \brief Start the batch processing
|
2243 |
2240 |
///
|
2244 |
2241 |
/// This function starts the batch processing.
|
2245 |
2242 |
void run() {
|
2246 |
2243 |
|
2247 |
2244 |
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
|
2248 |
2245 |
|
2249 |
2246 |
std::set<std::string> extra_sections;
|
2250 |
2247 |
|
2251 |
2248 |
line_num = 0;
|
2252 |
2249 |
readLine();
|
2253 |
2250 |
skipSection();
|
2254 |
2251 |
|
2255 |
2252 |
while (readSuccess()) {
|
2256 |
2253 |
try {
|
2257 |
2254 |
char c;
|
2258 |
2255 |
std::string section, caption;
|
2259 |
2256 |
line >> c;
|
2260 |
2257 |
_reader_bits::readToken(line, section);
|
2261 |
2258 |
_reader_bits::readToken(line, caption);
|
2262 |
2259 |
|
2263 |
2260 |
if (line >> c)
|
2264 |
|
throw FormatError("Extra character on the end of line");
|
|
2261 |
throw FormatError("Extra character at the end of line");
|
2265 |
2262 |
|
2266 |
2263 |
if (extra_sections.find(section) != extra_sections.end()) {
|
2267 |
2264 |
std::ostringstream msg;
|
2268 |
|
msg << "Multiple occurence of section " << section;
|
|
2265 |
msg << "Multiple occurence of section: " << section;
|
2269 |
2266 |
throw FormatError(msg.str());
|
2270 |
2267 |
}
|
2271 |
2268 |
Sections::iterator it = _sections.find(section);
|
2272 |
2269 |
if (it != _sections.end()) {
|
2273 |
2270 |
extra_sections.insert(section);
|
2274 |
2271 |
it->second->process(*_is, line_num);
|
2275 |
2272 |
}
|
2276 |
2273 |
readLine();
|
2277 |
2274 |
skipSection();
|
2278 |
2275 |
} catch (FormatError& error) {
|
2279 |
2276 |
error.line(line_num);
|
2280 |
2277 |
error.file(_filename);
|
2281 |
2278 |
throw;
|
2282 |
2279 |
}
|
2283 |
2280 |
}
|
2284 |
2281 |
for (Sections::iterator it = _sections.begin();
|
2285 |
2282 |
it != _sections.end(); ++it) {
|
2286 |
2283 |
if (extra_sections.find(it->first) == extra_sections.end()) {
|
2287 |
2284 |
std::ostringstream os;
|
2288 |
2285 |
os << "Cannot find section: " << it->first;
|
2289 |
2286 |
throw FormatError(os.str());
|
2290 |
2287 |
}
|
2291 |
2288 |
}
|
2292 |
2289 |
}
|
2293 |
2290 |
|
2294 |
2291 |
/// @}
|
2295 |
2292 |
|
2296 |
2293 |
};
|
2297 |
2294 |
|
2298 |
2295 |
/// \brief Return a \ref SectionReader class
|
2299 |
2296 |
///
|
2300 |
2297 |
/// This function just returns a \ref SectionReader class.
|
2301 |
2298 |
/// \relates SectionReader
|
2302 |
2299 |
inline SectionReader sectionReader(std::istream& is) {
|
2303 |
2300 |
SectionReader tmp(is);
|
2304 |
2301 |
return tmp;
|
2305 |
2302 |
}
|
2306 |
2303 |
|
2307 |
2304 |
/// \brief Return a \ref SectionReader class
|
2308 |
2305 |
///
|
2309 |
2306 |
/// This function just returns a \ref SectionReader class.
|
2310 |
2307 |
/// \relates SectionReader
|
2311 |
2308 |
inline SectionReader sectionReader(const std::string& fn) {
|
2312 |
2309 |
SectionReader tmp(fn);
|
2313 |
2310 |
return tmp;
|
2314 |
2311 |
}
|
2315 |
2312 |
|
2316 |
2313 |
/// \brief Return a \ref SectionReader class
|
2317 |
2314 |
///
|
2318 |
2315 |
/// This function just returns a \ref SectionReader class.
|
2319 |
2316 |
/// \relates SectionReader
|
2320 |
2317 |
inline SectionReader sectionReader(const char* fn) {
|
2321 |
2318 |
SectionReader tmp(fn);
|
2322 |
2319 |
return tmp;
|
2323 |
2320 |
}
|
2324 |
2321 |
|
2325 |
2322 |
/// \ingroup lemon_io
|
2326 |
2323 |
///
|
2327 |
2324 |
/// \brief Reader for the contents of the \ref lgf-format "LGF" file
|
2328 |
2325 |
///
|
2329 |
2326 |
/// This class can be used to read the sections, the map names and
|
2330 |
2327 |
/// the attributes from a file. Usually, the LEMON programs know
|
2331 |
2328 |
/// that, which type of graph, which maps and which attributes
|
2332 |
2329 |
/// should be read from a file, but in general tools (like glemon)
|
2333 |
2330 |
/// the contents of an LGF file should be guessed somehow. This class
|
2334 |
2331 |
/// reads the graph and stores the appropriate information for
|
2335 |
2332 |
/// reading the graph.
|
2336 |
2333 |
///
|
2337 |
2334 |
///\code
|
2338 |
2335 |
/// LgfContents contents("graph.lgf");
|
2339 |
2336 |
/// contents.run();
|
2340 |
2337 |
///
|
2341 |
2338 |
/// // Does it contain any node section and arc section?
|
2342 |
2339 |
/// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
|
2343 |
2340 |
/// std::cerr << "Failure, cannot find graph." << std::endl;
|
2344 |
2341 |
/// return -1;
|
2345 |
2342 |
/// }
|
2346 |
2343 |
/// std::cout << "The name of the default node section: "
|
2347 |
2344 |
/// << contents.nodeSection(0) << std::endl;
|
2348 |
2345 |
/// std::cout << "The number of the arc maps: "
|
2349 |
2346 |
/// << contents.arcMaps(0).size() << std::endl;
|
2350 |
2347 |
/// std::cout << "The name of second arc map: "
|
2351 |
2348 |
/// << contents.arcMaps(0)[1] << std::endl;
|
2352 |
2349 |
///\endcode
|
2353 |
2350 |
class LgfContents {
|
2354 |
2351 |
private:
|
2355 |
2352 |
|
2356 |
2353 |
std::istream* _is;
|
2357 |
2354 |
bool local_is;
|
2358 |
2355 |
|
2359 |
2356 |
std::vector<std::string> _node_sections;
|
2360 |
2357 |
std::vector<std::string> _edge_sections;
|
2361 |
2358 |
std::vector<std::string> _attribute_sections;
|
2362 |
2359 |
std::vector<std::string> _extra_sections;
|
2363 |
2360 |
|
2364 |
2361 |
std::vector<bool> _arc_sections;
|
2365 |
2362 |
|
2366 |
2363 |
std::vector<std::vector<std::string> > _node_maps;
|
2367 |
2364 |
std::vector<std::vector<std::string> > _edge_maps;
|
2368 |
2365 |
|
2369 |
2366 |
std::vector<std::vector<std::string> > _attributes;
|
2370 |
2367 |
|
2371 |
2368 |
|
2372 |
2369 |
int line_num;
|
2373 |
2370 |
std::istringstream line;
|
2374 |
2371 |
|
2375 |
2372 |
public:
|
2376 |
2373 |
|
2377 |
2374 |
/// \brief Constructor
|
2378 |
2375 |
///
|
2379 |
2376 |
/// Construct an \e LGF contents reader, which reads from the given
|
2380 |
2377 |
/// input stream.
|
2381 |
2378 |
LgfContents(std::istream& is)
|
2382 |
2379 |
: _is(&is), local_is(false) {}
|
2383 |
2380 |
|
2384 |
2381 |
/// \brief Constructor
|
2385 |
2382 |
///
|
2386 |
2383 |
/// Construct an \e LGF contents reader, which reads from the given
|
2387 |
2384 |
/// file.
|
2388 |
2385 |
LgfContents(const std::string& fn)
|
2389 |
2386 |
: _is(new std::ifstream(fn.c_str())), local_is(true) {
|
2390 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
2387 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
2391 |
2388 |
}
|
2392 |
2389 |
|
2393 |
2390 |
/// \brief Constructor
|
2394 |
2391 |
///
|
2395 |
2392 |
/// Construct an \e LGF contents reader, which reads from the given
|
2396 |
2393 |
/// file.
|
2397 |
2394 |
LgfContents(const char* fn)
|
2398 |
2395 |
: _is(new std::ifstream(fn)), local_is(true) {
|
2399 |
|
if (!(*_is)) throw IoError(fn, "Cannot open file");
|
|
2396 |
if (!(*_is)) throw IoError("Cannot open file", fn);
|
2400 |
2397 |
}
|
2401 |
2398 |
|
2402 |
2399 |
/// \brief Destructor
|
2403 |
2400 |
~LgfContents() {
|
2404 |
2401 |
if (local_is) delete _is;
|
2405 |
2402 |
}
|
2406 |
2403 |
|
2407 |
2404 |
private:
|
2408 |
2405 |
|
2409 |
2406 |
LgfContents(const LgfContents&);
|
2410 |
2407 |
LgfContents& operator=(const LgfContents&);
|
2411 |
2408 |
|
2412 |
2409 |
public:
|
2413 |
2410 |
|
2414 |
2411 |
|
2415 |
2412 |
/// \name Node sections
|
2416 |
2413 |
/// @{
|
2417 |
2414 |
|
2418 |
2415 |
/// \brief Gives back the number of node sections in the file.
|
2419 |
2416 |
///
|
2420 |
2417 |
/// Gives back the number of node sections in the file.
|
2421 |
2418 |
int nodeSectionNum() const {
|
2422 |
2419 |
return _node_sections.size();
|
2423 |
2420 |
}
|
2424 |
2421 |
|
2425 |
2422 |
/// \brief Returns the node section name at the given position.
|
2426 |
2423 |
///
|
2427 |
2424 |
/// Returns the node section name at the given position.
|
2428 |
2425 |
const std::string& nodeSection(int i) const {
|
2429 |
2426 |
return _node_sections[i];
|
2430 |
2427 |
}
|
2431 |
2428 |
|
2432 |
2429 |
/// \brief Gives back the node maps for the given section.
|
2433 |
2430 |
///
|
2434 |
2431 |
/// Gives back the node maps for the given section.
|
2435 |
2432 |
const std::vector<std::string>& nodeMapNames(int i) const {
|
2436 |
2433 |
return _node_maps[i];
|
2437 |
2434 |
}
|
2438 |
2435 |
|
2439 |
2436 |
/// @}
|
2440 |
2437 |
|
2441 |
2438 |
/// \name Arc/Edge sections
|
2442 |
2439 |
/// @{
|
2443 |
2440 |
|
2444 |
2441 |
/// \brief Gives back the number of arc/edge sections in the file.
|
2445 |
2442 |
///
|
2446 |
2443 |
/// Gives back the number of arc/edge sections in the file.
|
2447 |
2444 |
/// \note It is synonym of \c edgeSectionNum().
|
2448 |
2445 |
int arcSectionNum() const {
|
2449 |
2446 |
return _edge_sections.size();
|
2450 |
2447 |
}
|
2451 |
2448 |
|
2452 |
2449 |
/// \brief Returns the arc/edge section name at the given position.
|
2453 |
2450 |
///
|
2454 |
2451 |
/// Returns the arc/edge section name at the given position.
|
2455 |
2452 |
/// \note It is synonym of \c edgeSection().
|
2456 |
2453 |
const std::string& arcSection(int i) const {
|
2457 |
2454 |
return _edge_sections[i];
|
2458 |
2455 |
}
|
2459 |
2456 |
|
2460 |
2457 |
/// \brief Gives back the arc/edge maps for the given section.
|
2461 |
2458 |
///
|
2462 |
2459 |
/// Gives back the arc/edge maps for the given section.
|
2463 |
2460 |
/// \note It is synonym of \c edgeMapNames().
|
2464 |
2461 |
const std::vector<std::string>& arcMapNames(int i) const {
|
2465 |
2462 |
return _edge_maps[i];
|
2466 |
2463 |
}
|
2467 |
2464 |
|
2468 |
2465 |
/// @}
|
2469 |
2466 |
|
2470 |
2467 |
/// \name Synonyms
|
2471 |
2468 |
/// @{
|
2472 |
2469 |
|
2473 |
2470 |
/// \brief Gives back the number of arc/edge sections in the file.
|
2474 |
2471 |
///
|
2475 |
2472 |
/// Gives back the number of arc/edge sections in the file.
|
2476 |
2473 |
/// \note It is synonym of \c arcSectionNum().
|
2477 |
2474 |
int edgeSectionNum() const {
|
2478 |
2475 |
return _edge_sections.size();
|
2479 |
2476 |
}
|
2480 |
2477 |
|
2481 |
2478 |
/// \brief Returns the section name at the given position.
|
2482 |
2479 |
///
|
2483 |
2480 |
/// Returns the section name at the given position.
|
2484 |
2481 |
/// \note It is synonym of \c arcSection().
|
2485 |
2482 |
const std::string& edgeSection(int i) const {
|
2486 |
2483 |
return _edge_sections[i];
|
2487 |
2484 |
}
|
2488 |
2485 |
|
2489 |
2486 |
/// \brief Gives back the edge maps for the given section.
|
2490 |
2487 |
///
|
2491 |
2488 |
/// Gives back the edge maps for the given section.
|
2492 |
2489 |
/// \note It is synonym of \c arcMapNames().
|
2493 |
2490 |
const std::vector<std::string>& edgeMapNames(int i) const {
|
2494 |
2491 |
return _edge_maps[i];
|
2495 |
2492 |
}
|