3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2008
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 ///\brief Lemon Graph Format reader.
24 #ifndef LEMON_LGF_READER_H
25 #define LEMON_LGF_READER_H
34 #include <lemon/assert.h>
35 #include <lemon/graph_utils.h>
37 #include <lemon/lgf_writer.h>
39 #include <lemon/concept_check.h>
40 #include <lemon/concepts/maps.h>
44 namespace _reader_bits {
46 template <typename Value>
47 struct DefaultConverter {
48 Value operator()(const std::string& str) {
49 std::istringstream is(str);
54 if (is >> std::ws >> c) {
55 throw DataFormatError("Remaining characters in token");
62 struct DefaultConverter<std::string> {
63 std::string operator()(const std::string& str) {
68 template <typename _Item>
69 class MapStorageBase {
75 virtual ~MapStorageBase() {}
77 virtual void set(const Item& item, const std::string& value) = 0;
81 template <typename _Item, typename _Map,
82 typename _Converter = DefaultConverter<typename _Map::Value> >
83 class MapStorage : public MapStorageBase<_Item> {
86 typedef _Converter Converter;
94 MapStorage(Map& map, const Converter& converter = Converter())
95 : _map(map), _converter(converter) {}
96 virtual ~MapStorage() {}
98 virtual void set(const Item& item ,const std::string& value) {
99 _map.set(item, _converter(value));
103 template <typename _Graph, bool _dir, typename _Map,
104 typename _Converter = DefaultConverter<typename _Map::Value> >
105 class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
108 typedef _Converter Converter;
109 typedef _Graph Graph;
110 typedef typename Graph::Edge Item;
111 static const bool dir = _dir;
116 Converter _converter;
119 GraphArcMapStorage(const Graph& graph, Map& map,
120 const Converter& converter = Converter())
121 : _graph(graph), _map(map), _converter(converter) {}
122 virtual ~GraphArcMapStorage() {}
124 virtual void set(const Item& item ,const std::string& value) {
125 _map.set(_graph.direct(item, dir), _converter(value));
129 class ValueStorageBase {
131 ValueStorageBase() {}
132 virtual ~ValueStorageBase() {}
134 virtual void set(const std::string&) = 0;
137 template <typename _Value, typename _Converter = DefaultConverter<_Value> >
138 class ValueStorage : public ValueStorageBase {
140 typedef _Value Value;
141 typedef _Converter Converter;
145 Converter _converter;
148 ValueStorage(Value& value, const Converter& converter = Converter())
149 : _value(value), _converter(converter) {}
151 virtual void set(const std::string& value) {
152 _value = _converter(value);
156 template <typename Value>
157 struct MapLookUpConverter {
158 const std::map<std::string, Value>& _map;
160 MapLookUpConverter(const std::map<std::string, Value>& map)
163 Value operator()(const std::string& str) {
164 typename std::map<std::string, Value>::const_iterator it =
166 if (it == _map.end()) {
167 std::ostringstream msg;
168 msg << "Item not found: " << str;
169 throw DataFormatError(msg.str().c_str());
175 template <typename Graph>
176 struct GraphArcLookUpConverter {
178 const std::map<std::string, typename Graph::Edge>& _map;
180 GraphArcLookUpConverter(const Graph& graph,
181 const std::map<std::string,
182 typename Graph::Edge>& map)
183 : _graph(graph), _map(map) {}
185 typename Graph::Arc operator()(const std::string& str) {
186 if (str.empty() || (str[0] != '+' && str[0] != '-')) {
187 throw DataFormatError("Item must start with '+' or '-'");
189 typename std::map<std::string, typename Graph::Edge>
190 ::const_iterator it = _map.find(str.substr(1));
191 if (it == _map.end()) {
192 throw DataFormatError("Item not found");
194 return _graph.direct(it->second, str[0] == '+');
198 bool isWhiteSpace(char c) {
199 return c == ' ' || c == '\t' || c == '\v' ||
200 c == '\n' || c == '\r' || c == '\f';
204 return '0' <= c && c <='7';
207 int valueOct(char c) {
208 LEMON_ASSERT(isOct(c), "The character is not octal.");
213 return ('0' <= c && c <= '9') ||
214 ('a' <= c && c <= 'z') ||
215 ('A' <= c && c <= 'Z');
218 int valueHex(char c) {
219 LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
220 if ('0' <= c && c <= '9') return c - '0';
221 if ('a' <= c && c <= 'z') return c - 'a' + 10;
225 bool isIdentifierFirstChar(char c) {
226 return ('a' <= c && c <= 'z') ||
227 ('A' <= c && c <= 'Z') || c == '_';
230 bool isIdentifierChar(char c) {
231 return isIdentifierFirstChar(c) ||
232 ('0' <= c && c <= '9');
235 char readEscape(std::istream& is) {
238 throw DataFormatError("Escape format error");
266 if (!is.get(c) || !isHex(c))
267 throw DataFormatError("Escape format error");
268 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
269 else code = code * 16 + valueHex(c);
276 throw DataFormatError("Escape format error");
277 else if (code = valueOct(c), !is.get(c) || !isOct(c))
279 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
281 else code = code * 8 + valueOct(c);
287 std::istream& readToken(std::istream& is, std::string& str) {
288 std::ostringstream os;
297 while (is.get(c) && c != '\"') {
303 throw DataFormatError("Quoted format error");
306 while (is.get(c) && !isWhiteSpace(c)) {
323 virtual ~Section() {}
324 virtual void process(std::istream& is, int& line_num) = 0;
327 template <typename Functor>
328 class LineSection : public Section {
335 LineSection(const Functor& functor) : _functor(functor) {}
336 virtual ~LineSection() {}
338 virtual void process(std::istream& is, int& line_num) {
341 while (is.get(c) && c != '@') {
344 } else if (c == '#') {
347 } else if (!isWhiteSpace(c)) {
354 if (is) is.putback(c);
355 else if (is.eof()) is.clear();
359 template <typename Functor>
360 class StreamSection : public Section {
367 StreamSection(const Functor& functor) : _functor(functor) {}
368 virtual ~StreamSection() {}
370 virtual void process(std::istream& is, int& line_num) {
371 _functor(is, line_num);
374 while (is.get(c) && c != '@') {
377 } else if (!isWhiteSpace(c)) {
382 if (is) is.putback(c);
383 else if (is.eof()) is.clear();
389 /// \ingroup lemon_io
391 /// \brief LGF reader for directed graphs
393 /// This utility reads an \ref lgf-format "LGF" file.
395 /// The reading method does a batch processing. The user creates a
396 /// reader object, then various reading rules can be added to the
397 /// reader, and eventually the reading is executed with the \c run()
398 /// member function. A map reading rule can be added to the reader
399 /// with the \c nodeMap() or \c arcMap() members. An optional
400 /// converter parameter can also be added as a standard functor
401 /// converting from std::string to the value type of the map. If it
402 /// is set, it will determine how the tokens in the file should be
403 /// is converted to the map's value type. If the functor is not set,
404 /// then a default conversion will be used. One map can be read into
405 /// multiple map objects at the same time. The \c attribute(), \c
406 /// node() and \c arc() functions are used to add attribute reading
410 /// DigraphReader<Digraph>(std::cin, digraph).
411 /// nodeMap("coordinates", coord_map).
412 /// arcMap("capacity", cap_map).
413 /// node("source", src).
414 /// node("target", trg).
415 /// attribute("caption", caption).
419 /// By default the reader uses the first section in the file of the
420 /// proper type. If a section has an optional name, then it can be
421 /// selected for reading by giving an optional name parameter to the
422 /// \c nodes(), \c arcs() or \c attributes() functions. The readers
423 /// also can load extra sections with the \c sectionLines() and
424 /// sectionStream() functions.
426 /// The \c useNodes() and \c useArcs() functions are used to tell the reader
427 /// that the nodes or arcs should not be constructed (added to the
428 /// graph) during the reading, but instead the label map of the items
429 /// are given as a parameter of these functions. An
430 /// application of these function is multipass reading, which is
431 /// important if two \e \@arcs sections must be read from the
432 /// file. In this example the first phase would read the node set and one
433 /// of the arc sets, while the second phase would read the second arc
434 /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
435 /// The previously read label node map should be passed to the \c
436 /// useNodes() functions. Another application of multipass reading when
437 /// paths are given as a node map or an arc map. It is impossible read this in
438 /// a single pass, because the arcs are not constructed when the node
440 template <typename _Digraph>
441 class DigraphReader {
444 typedef _Digraph Digraph;
445 TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
455 std::string _nodes_caption;
456 std::string _arcs_caption;
457 std::string _attributes_caption;
459 typedef std::map<std::string, Node> NodeIndex;
460 NodeIndex _node_index;
461 typedef std::map<std::string, Arc> ArcIndex;
464 typedef std::vector<std::pair<std::string,
465 _reader_bits::MapStorageBase<Node>*> > NodeMaps;
468 typedef std::vector<std::pair<std::string,
469 _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
472 typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
474 Attributes _attributes;
476 typedef std::map<std::string, _reader_bits::Section*> Sections;
483 std::istringstream line;
487 /// \brief Constructor
489 /// Construct a directed graph reader, which reads from the given
491 DigraphReader(std::istream& is, Digraph& digraph)
492 : _is(&is), local_is(false), _digraph(digraph),
493 _use_nodes(false), _use_arcs(false) {}
495 /// \brief Constructor
497 /// Construct a directed graph reader, which reads from the given
499 DigraphReader(const std::string& fn, Digraph& digraph)
500 : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
501 _use_nodes(false), _use_arcs(false) {}
503 /// \brief Constructor
505 /// Construct a directed graph reader, which reads from the given
507 DigraphReader(const char* fn, Digraph& digraph)
508 : _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
509 _use_nodes(false), _use_arcs(false) {}
511 /// \brief Copy constructor
513 /// The copy constructor transfers all data from the other reader,
514 /// therefore the copied reader will not be usable more.
515 DigraphReader(DigraphReader& other)
516 : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
517 _use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
520 other.local_is = false;
522 _node_index.swap(other._node_index);
523 _arc_index.swap(other._arc_index);
525 _node_maps.swap(other._node_maps);
526 _arc_maps.swap(other._arc_maps);
527 _attributes.swap(other._attributes);
529 _nodes_caption = other._nodes_caption;
530 _arcs_caption = other._arcs_caption;
531 _attributes_caption = other._attributes_caption;
533 _sections.swap(other._sections);
536 /// \brief Destructor
538 for (typename NodeMaps::iterator it = _node_maps.begin();
539 it != _node_maps.end(); ++it) {
543 for (typename ArcMaps::iterator it = _arc_maps.begin();
544 it != _arc_maps.end(); ++it) {
548 for (typename Attributes::iterator it = _attributes.begin();
549 it != _attributes.end(); ++it) {
553 for (typename Sections::iterator it = _sections.begin();
554 it != _sections.end(); ++it) {
566 DigraphReader& operator=(const DigraphReader&);
570 /// \name Reading rules
573 /// \brief Node map reading rule
575 /// Add a node map reading rule to the reader.
576 template <typename Map>
577 DigraphReader& nodeMap(const std::string& caption, Map& map) {
578 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
579 _reader_bits::MapStorageBase<Node>* storage =
580 new _reader_bits::MapStorage<Node, Map>(map);
581 _node_maps.push_back(std::make_pair(caption, storage));
585 /// \brief Node map reading rule
587 /// Add a node map reading rule with specialized converter to the
589 template <typename Map, typename Converter>
590 DigraphReader& nodeMap(const std::string& caption, Map& map,
591 const Converter& converter = Converter()) {
592 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
593 _reader_bits::MapStorageBase<Node>* storage =
594 new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
595 _node_maps.push_back(std::make_pair(caption, storage));
599 /// \brief Arc map reading rule
601 /// Add an arc map reading rule to the reader.
602 template <typename Map>
603 DigraphReader& arcMap(const std::string& caption, Map& map) {
604 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
605 _reader_bits::MapStorageBase<Arc>* storage =
606 new _reader_bits::MapStorage<Arc, Map>(map);
607 _arc_maps.push_back(std::make_pair(caption, storage));
611 /// \brief Arc map reading rule
613 /// Add an arc map reading rule with specialized converter to the
615 template <typename Map, typename Converter>
616 DigraphReader& arcMap(const std::string& caption, Map& map,
617 const Converter& converter = Converter()) {
618 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
619 _reader_bits::MapStorageBase<Arc>* storage =
620 new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
621 _arc_maps.push_back(std::make_pair(caption, storage));
625 /// \brief Attribute reading rule
627 /// Add an attribute reading rule to the reader.
628 template <typename Value>
629 DigraphReader& attribute(const std::string& caption, Value& value) {
630 _reader_bits::ValueStorageBase* storage =
631 new _reader_bits::ValueStorage<Value>(value);
632 _attributes.insert(std::make_pair(caption, storage));
636 /// \brief Attribute reading rule
638 /// Add an attribute reading rule with specialized converter to the
640 template <typename Value, typename Converter>
641 DigraphReader& attribute(const std::string& caption, Value& value,
642 const Converter& converter = Converter()) {
643 _reader_bits::ValueStorageBase* storage =
644 new _reader_bits::ValueStorage<Value, Converter>(value, converter);
645 _attributes.insert(std::make_pair(caption, storage));
649 /// \brief Node reading rule
651 /// Add a node reading rule to reader.
652 DigraphReader& node(const std::string& caption, Node& node) {
653 typedef _reader_bits::MapLookUpConverter<Node> Converter;
654 Converter converter(_node_index);
655 _reader_bits::ValueStorageBase* storage =
656 new _reader_bits::ValueStorage<Node, Converter>(node, converter);
657 _attributes.insert(std::make_pair(caption, storage));
661 /// \brief Arc reading rule
663 /// Add an arc reading rule to reader.
664 DigraphReader& arc(const std::string& caption, Arc& arc) {
665 typedef _reader_bits::MapLookUpConverter<Arc> Converter;
666 Converter converter(_arc_index);
667 _reader_bits::ValueStorageBase* storage =
668 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
669 _attributes.insert(std::make_pair(caption, storage));
675 /// \name Select section by name
678 /// \brief Set \c \@nodes section to be read
680 /// Set \c \@nodes section to be read
681 DigraphReader& nodes(const std::string& caption) {
682 _nodes_caption = caption;
686 /// \brief Set \c \@arcs section to be read
688 /// Set \c \@arcs section to be read
689 DigraphReader& arcs(const std::string& caption) {
690 _arcs_caption = caption;
694 /// \brief Set \c \@attributes section to be read
696 /// Set \c \@attributes section to be read
697 DigraphReader& attributes(const std::string& caption) {
698 _attributes_caption = caption;
704 /// \name Section readers
707 /// \brief Add a section processor with line oriented reading
709 /// In the \e LGF file extra sections can be placed, which contain
710 /// any data in arbitrary format. These sections can be read with
711 /// this function line by line. The first parameter is the type
712 /// descriptor of the section, the second is a functor, which
713 /// takes just one \c std::string parameter. At the reading
714 /// process, each line of the section will be given to the functor
715 /// object. However, the empty lines and the comment lines are
716 /// filtered out, and the leading whitespaces are stipped from
717 /// each processed string.
719 /// For example let's see a section, which contain several
720 /// integers, which should be inserted into a vector.
728 /// The functor is implemented as an struct:
730 /// struct NumberSection {
731 /// std::vector<int>& _data;
732 /// NumberSection(std::vector<int>& data) : _data(data) {}
733 /// void operator()(const std::string& line) {
734 /// std::istringstream ls(line);
736 /// while (ls >> value) _data.push_back(value);
742 /// reader.sectionLines("numbers", NumberSection(vec));
744 template <typename Functor>
745 DigraphReader& sectionLines(const std::string& type, Functor functor) {
746 LEMON_ASSERT(!type.empty(), "Type is not empty.");
747 LEMON_ASSERT(_sections.find(type) == _sections.end(),
748 "Multiple reading of section.");
749 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
750 type != "attributes", "Multiple reading of section.");
751 _sections.insert(std::make_pair(type,
752 new _reader_bits::LineSection<Functor>(functor)));
757 /// \brief Add a section processor with stream oriented reading
759 /// In the \e LGF file extra sections can be placed, which contain
760 /// any data in arbitrary format. These sections can be read
761 /// directly with this function. The first parameter is the type
762 /// of the section, the second is a functor, which takes an \c
763 /// std::istream& and an int& parameter, the latter regard to the
764 /// line number of stream. The functor can read the input while
765 /// the section go on, and the line number should be modified
767 template <typename Functor>
768 DigraphReader& sectionStream(const std::string& type, Functor functor) {
769 LEMON_ASSERT(!type.empty(), "Type is not empty.");
770 LEMON_ASSERT(_sections.find(type) == _sections.end(),
771 "Multiple reading of section.");
772 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
773 type != "attributes", "Multiple reading of section.");
774 _sections.insert(std::make_pair(type,
775 new _reader_bits::StreamSection<Functor>(functor)));
781 /// \name Using previously constructed node or arc set
784 /// \brief Use previously constructed node set
786 /// Use previously constructed node set, and specify the node
788 template <typename Map>
789 DigraphReader& useNodes(const Map& map) {
790 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
791 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
793 _writer_bits::DefaultConverter<typename Map::Value> converter;
794 for (NodeIt n(_digraph); n != INVALID; ++n) {
795 _node_index.insert(std::make_pair(converter(map[n]), n));
800 /// \brief Use previously constructed node set
802 /// Use previously constructed node set, and specify the node
803 /// label map and a functor which converts the label map values to
805 template <typename Map, typename Converter>
806 DigraphReader& useNodes(const Map& map,
807 const Converter& converter = Converter()) {
808 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
809 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
811 for (NodeIt n(_digraph); n != INVALID; ++n) {
812 _node_index.insert(std::make_pair(converter(map[n]), n));
817 /// \brief Use previously constructed arc set
819 /// Use previously constructed arc set, and specify the arc
821 template <typename Map>
822 DigraphReader& useArcs(const Map& map) {
823 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
824 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
826 _writer_bits::DefaultConverter<typename Map::Value> converter;
827 for (ArcIt a(_digraph); a != INVALID; ++a) {
828 _arc_index.insert(std::make_pair(converter(map[a]), a));
833 /// \brief Use previously constructed arc set
835 /// Use previously constructed arc set, and specify the arc
836 /// label map and a functor which converts the label map values to
838 template <typename Map, typename Converter>
839 DigraphReader& useArcs(const Map& map,
840 const Converter& converter = Converter()) {
841 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
842 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
844 for (ArcIt a(_digraph); a != INVALID; ++a) {
845 _arc_index.insert(std::make_pair(converter(map[a]), a));
856 while(++line_num, std::getline(*_is, str)) {
857 line.clear(); line.str(str);
859 if (line >> std::ws >> c && c != '#') {
868 return static_cast<bool>(*_is);
873 while (readSuccess() && line >> c && c != '@') {
881 std::vector<int> map_index(_node_maps.size());
882 int map_num, label_index;
885 throw DataFormatError("Cannot find map captions");
888 std::map<std::string, int> maps;
892 while (_reader_bits::readToken(line, map)) {
893 if (maps.find(map) != maps.end()) {
894 std::ostringstream msg;
895 msg << "Multiple occurence of node map: " << map;
896 throw DataFormatError(msg.str().c_str());
898 maps.insert(std::make_pair(map, index));
902 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
903 std::map<std::string, int>::iterator jt =
904 maps.find(_node_maps[i].first);
905 if (jt == maps.end()) {
906 std::ostringstream msg;
907 msg << "Map not found in file: " << _node_maps[i].first;
908 throw DataFormatError(msg.str().c_str());
910 map_index[i] = jt->second;
914 std::map<std::string, int>::iterator jt = maps.find("label");
915 if (jt == maps.end())
916 throw DataFormatError("Label map not found in file");
917 label_index = jt->second;
919 map_num = maps.size();
923 while (readLine() && line >> c && c != '@') {
926 std::vector<std::string> tokens(map_num);
927 for (int i = 0; i < map_num; ++i) {
928 if (!_reader_bits::readToken(line, tokens[i])) {
929 std::ostringstream msg;
930 msg << "Column not found (" << i + 1 << ")";
931 throw DataFormatError(msg.str().c_str());
934 if (line >> std::ws >> c)
935 throw DataFormatError("Extra character on the end of line");
939 n = _digraph.addNode();
940 _node_index.insert(std::make_pair(tokens[label_index], n));
942 typename std::map<std::string, Node>::iterator it =
943 _node_index.find(tokens[label_index]);
944 if (it == _node_index.end()) {
945 std::ostringstream msg;
946 msg << "Node with label not found: " << tokens[label_index];
947 throw DataFormatError(msg.str().c_str());
952 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
953 _node_maps[i].second->set(n, tokens[map_index[i]]);
964 std::vector<int> map_index(_arc_maps.size());
965 int map_num, label_index;
968 throw DataFormatError("Cannot find map captions");
971 std::map<std::string, int> maps;
975 while (_reader_bits::readToken(line, map)) {
976 if (maps.find(map) != maps.end()) {
977 std::ostringstream msg;
978 msg << "Multiple occurence of arc map: " << map;
979 throw DataFormatError(msg.str().c_str());
981 maps.insert(std::make_pair(map, index));
985 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
986 std::map<std::string, int>::iterator jt =
987 maps.find(_arc_maps[i].first);
988 if (jt == maps.end()) {
989 std::ostringstream msg;
990 msg << "Map not found in file: " << _arc_maps[i].first;
991 throw DataFormatError(msg.str().c_str());
993 map_index[i] = jt->second;
997 std::map<std::string, int>::iterator jt = maps.find("label");
998 if (jt == maps.end())
999 throw DataFormatError("Label map not found in file");
1000 label_index = jt->second;
1002 map_num = maps.size();
1006 while (readLine() && line >> c && c != '@') {
1009 std::string source_token;
1010 std::string target_token;
1012 if (!_reader_bits::readToken(line, source_token))
1013 throw DataFormatError("Source not found");
1015 if (!_reader_bits::readToken(line, target_token))
1016 throw DataFormatError("Source not found");
1018 std::vector<std::string> tokens(map_num);
1019 for (int i = 0; i < map_num; ++i) {
1020 if (!_reader_bits::readToken(line, tokens[i])) {
1021 std::ostringstream msg;
1022 msg << "Column not found (" << i + 1 << ")";
1023 throw DataFormatError(msg.str().c_str());
1026 if (line >> std::ws >> c)
1027 throw DataFormatError("Extra character on the end of line");
1032 typename NodeIndex::iterator it;
1034 it = _node_index.find(source_token);
1035 if (it == _node_index.end()) {
1036 std::ostringstream msg;
1037 msg << "Item not found: " << source_token;
1038 throw DataFormatError(msg.str().c_str());
1040 Node source = it->second;
1042 it = _node_index.find(target_token);
1043 if (it == _node_index.end()) {
1044 std::ostringstream msg;
1045 msg << "Item not found: " << target_token;
1046 throw DataFormatError(msg.str().c_str());
1048 Node target = it->second;
1050 a = _digraph.addArc(source, target);
1051 _arc_index.insert(std::make_pair(tokens[label_index], a));
1053 typename std::map<std::string, Arc>::iterator it =
1054 _arc_index.find(tokens[label_index]);
1055 if (it == _arc_index.end()) {
1056 std::ostringstream msg;
1057 msg << "Arc with label not found: " << tokens[label_index];
1058 throw DataFormatError(msg.str().c_str());
1063 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
1064 _arc_maps[i].second->set(a, tokens[map_index[i]]);
1068 if (readSuccess()) {
1073 void readAttributes() {
1075 std::set<std::string> read_attr;
1078 while (readLine() && line >> c && c != '@') {
1081 std::string attr, token;
1082 if (!_reader_bits::readToken(line, attr))
1083 throw DataFormatError("Attribute name not found");
1084 if (!_reader_bits::readToken(line, token))
1085 throw DataFormatError("Attribute value not found");
1087 throw DataFormatError("Extra character on the end of line");
1090 std::set<std::string>::iterator it = read_attr.find(attr);
1091 if (it != read_attr.end()) {
1092 std::ostringstream msg;
1093 msg << "Multiple occurence of attribute " << attr;
1094 throw DataFormatError(msg.str().c_str());
1096 read_attr.insert(attr);
1100 typename Attributes::iterator it = _attributes.lower_bound(attr);
1101 while (it != _attributes.end() && it->first == attr) {
1102 it->second->set(token);
1108 if (readSuccess()) {
1111 for (typename Attributes::iterator it = _attributes.begin();
1112 it != _attributes.end(); ++it) {
1113 if (read_attr.find(it->first) == read_attr.end()) {
1114 std::ostringstream msg;
1115 msg << "Attribute not found in file: " << it->first;
1116 throw DataFormatError(msg.str().c_str());
1123 /// \name Execution of the reader
1126 /// \brief Start the batch processing
1128 /// This function starts the batch processing
1130 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1132 throw DataFormatError("Cannot find file");
1135 bool nodes_done = false;
1136 bool arcs_done = false;
1137 bool attributes_done = false;
1138 std::set<std::string> extra_sections;
1144 while (readSuccess()) {
1147 std::string section, caption;
1149 _reader_bits::readToken(line, section);
1150 _reader_bits::readToken(line, caption);
1153 throw DataFormatError("Extra character on the end of line");
1155 if (section == "nodes" && !nodes_done) {
1156 if (_nodes_caption.empty() || _nodes_caption == caption) {
1160 } else if ((section == "arcs" || section == "edges") &&
1162 if (_arcs_caption.empty() || _arcs_caption == caption) {
1166 } else if (section == "attributes" && !attributes_done) {
1167 if (_attributes_caption.empty() || _attributes_caption == caption) {
1169 attributes_done = true;
1172 if (extra_sections.find(section) != extra_sections.end()) {
1173 std::ostringstream msg;
1174 msg << "Multiple occurence of section " << section;
1175 throw DataFormatError(msg.str().c_str());
1177 Sections::iterator it = _sections.find(section);
1178 if (it != _sections.end()) {
1179 extra_sections.insert(section);
1180 it->second->process(*_is, line_num);
1185 } catch (DataFormatError& error) {
1186 error.line(line_num);
1192 throw DataFormatError("Section @nodes not found");
1196 throw DataFormatError("Section @arcs not found");
1199 if (!attributes_done && !_attributes.empty()) {
1200 throw DataFormatError("Section @attributes not found");
1209 /// \relates DigraphReader
1210 template <typename Digraph>
1211 DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
1212 DigraphReader<Digraph> tmp(is, digraph);
1216 /// \relates DigraphReader
1217 template <typename Digraph>
1218 DigraphReader<Digraph> digraphReader(const std::string& fn,
1220 DigraphReader<Digraph> tmp(fn, digraph);
1224 /// \relates DigraphReader
1225 template <typename Digraph>
1226 DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
1227 DigraphReader<Digraph> tmp(fn, digraph);
1231 /// \ingroup lemon_io
1233 /// \brief LGF reader for undirected graphs
1235 /// This utility reads an \ref lgf-format "LGF" file.
1236 template <typename _Graph>
1240 typedef _Graph Graph;
1241 TEMPLATE_GRAPH_TYPEDEFS(Graph);
1251 std::string _nodes_caption;
1252 std::string _edges_caption;
1253 std::string _attributes_caption;
1255 typedef std::map<std::string, Node> NodeIndex;
1256 NodeIndex _node_index;
1257 typedef std::map<std::string, Edge> EdgeIndex;
1258 EdgeIndex _edge_index;
1260 typedef std::vector<std::pair<std::string,
1261 _reader_bits::MapStorageBase<Node>*> > NodeMaps;
1262 NodeMaps _node_maps;
1264 typedef std::vector<std::pair<std::string,
1265 _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
1266 EdgeMaps _edge_maps;
1268 typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
1270 Attributes _attributes;
1272 typedef std::map<std::string, _reader_bits::Section*> Sections;
1279 std::istringstream line;
1283 /// \brief Constructor
1285 /// Construct a undirected graph reader, which reads from the given
1287 GraphReader(std::istream& is, Graph& graph)
1288 : _is(&is), local_is(false), _graph(graph),
1289 _use_nodes(false), _use_edges(false) {}
1291 /// \brief Constructor
1293 /// Construct a undirected graph reader, which reads from the given
1295 GraphReader(const std::string& fn, Graph& graph)
1296 : _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
1297 _use_nodes(false), _use_edges(false) {}
1299 /// \brief Constructor
1301 /// Construct a undirected graph reader, which reads from the given
1303 GraphReader(const char* fn, Graph& graph)
1304 : _is(new std::ifstream(fn)), local_is(true), _graph(graph),
1305 _use_nodes(false), _use_edges(false) {}
1307 /// \brief Copy constructor
1309 /// The copy constructor transfers all data from the other reader,
1310 /// therefore the copied reader will not be usable more.
1311 GraphReader(GraphReader& other)
1312 : _is(other._is), local_is(other.local_is), _graph(other._graph),
1313 _use_nodes(other._use_nodes), _use_edges(other._use_edges) {
1316 other.local_is = false;
1318 _node_index.swap(other._node_index);
1319 _edge_index.swap(other._edge_index);
1321 _node_maps.swap(other._node_maps);
1322 _edge_maps.swap(other._edge_maps);
1323 _attributes.swap(other._attributes);
1325 _nodes_caption = other._nodes_caption;
1326 _edges_caption = other._edges_caption;
1327 _attributes_caption = other._attributes_caption;
1329 _sections.swap(other._sections);
1332 /// \brief Destructor
1334 for (typename NodeMaps::iterator it = _node_maps.begin();
1335 it != _node_maps.end(); ++it) {
1339 for (typename EdgeMaps::iterator it = _edge_maps.begin();
1340 it != _edge_maps.end(); ++it) {
1344 for (typename Attributes::iterator it = _attributes.begin();
1345 it != _attributes.end(); ++it) {
1349 for (typename Sections::iterator it = _sections.begin();
1350 it != _sections.end(); ++it) {
1362 GraphReader& operator=(const GraphReader&);
1366 /// \name Reading rules
1369 /// \brief Node map reading rule
1371 /// Add a node map reading rule to the reader.
1372 template <typename Map>
1373 GraphReader& nodeMap(const std::string& caption, Map& map) {
1374 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1375 _reader_bits::MapStorageBase<Node>* storage =
1376 new _reader_bits::MapStorage<Node, Map>(map);
1377 _node_maps.push_back(std::make_pair(caption, storage));
1381 /// \brief Node map reading rule
1383 /// Add a node map reading rule with specialized converter to the
1385 template <typename Map, typename Converter>
1386 GraphReader& nodeMap(const std::string& caption, Map& map,
1387 const Converter& converter = Converter()) {
1388 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1389 _reader_bits::MapStorageBase<Node>* storage =
1390 new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
1391 _node_maps.push_back(std::make_pair(caption, storage));
1395 /// \brief Edge map reading rule
1397 /// Add an edge map reading rule to the reader.
1398 template <typename Map>
1399 GraphReader& edgeMap(const std::string& caption, Map& map) {
1400 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1401 _reader_bits::MapStorageBase<Edge>* storage =
1402 new _reader_bits::MapStorage<Edge, Map>(map);
1403 _edge_maps.push_back(std::make_pair(caption, storage));
1407 /// \brief Edge map reading rule
1409 /// Add an edge map reading rule with specialized converter to the
1411 template <typename Map, typename Converter>
1412 GraphReader& edgeMap(const std::string& caption, Map& map,
1413 const Converter& converter = Converter()) {
1414 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1415 _reader_bits::MapStorageBase<Edge>* storage =
1416 new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
1417 _edge_maps.push_back(std::make_pair(caption, storage));
1421 /// \brief Arc map reading rule
1423 /// Add an arc map reading rule to the reader.
1424 template <typename Map>
1425 GraphReader& arcMap(const std::string& caption, Map& map) {
1426 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1427 _reader_bits::MapStorageBase<Edge>* forward_storage =
1428 new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
1429 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1430 _reader_bits::MapStorageBase<Edge>* backward_storage =
1431 new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
1432 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1436 /// \brief Arc map reading rule
1438 /// Add an arc map reading rule with specialized converter to the
1440 template <typename Map, typename Converter>
1441 GraphReader& arcMap(const std::string& caption, Map& map,
1442 const Converter& converter = Converter()) {
1443 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1444 _reader_bits::MapStorageBase<Edge>* forward_storage =
1445 new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
1446 (_graph, map, converter);
1447 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1448 _reader_bits::MapStorageBase<Edge>* backward_storage =
1449 new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
1450 (_graph, map, converter);
1451 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1455 /// \brief Attribute reading rule
1457 /// Add an attribute reading rule to the reader.
1458 template <typename Value>
1459 GraphReader& attribute(const std::string& caption, Value& value) {
1460 _reader_bits::ValueStorageBase* storage =
1461 new _reader_bits::ValueStorage<Value>(value);
1462 _attributes.insert(std::make_pair(caption, storage));
1466 /// \brief Attribute reading rule
1468 /// Add an attribute reading rule with specialized converter to the
1470 template <typename Value, typename Converter>
1471 GraphReader& attribute(const std::string& caption, Value& value,
1472 const Converter& converter = Converter()) {
1473 _reader_bits::ValueStorageBase* storage =
1474 new _reader_bits::ValueStorage<Value, Converter>(value, converter);
1475 _attributes.insert(std::make_pair(caption, storage));
1479 /// \brief Node reading rule
1481 /// Add a node reading rule to reader.
1482 GraphReader& node(const std::string& caption, Node& node) {
1483 typedef _reader_bits::MapLookUpConverter<Node> Converter;
1484 Converter converter(_node_index);
1485 _reader_bits::ValueStorageBase* storage =
1486 new _reader_bits::ValueStorage<Node, Converter>(node, converter);
1487 _attributes.insert(std::make_pair(caption, storage));
1491 /// \brief Edge reading rule
1493 /// Add an edge reading rule to reader.
1494 GraphReader& edge(const std::string& caption, Edge& edge) {
1495 typedef _reader_bits::MapLookUpConverter<Edge> Converter;
1496 Converter converter(_edge_index);
1497 _reader_bits::ValueStorageBase* storage =
1498 new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
1499 _attributes.insert(std::make_pair(caption, storage));
1503 /// \brief Arc reading rule
1505 /// Add an arc reading rule to reader.
1506 GraphReader& arc(const std::string& caption, Arc& arc) {
1507 typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
1508 Converter converter(_graph, _edge_index);
1509 _reader_bits::ValueStorageBase* storage =
1510 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
1511 _attributes.insert(std::make_pair(caption, storage));
1517 /// \name Select section by name
1520 /// \brief Set \c \@nodes section to be read
1522 /// Set \c \@nodes section to be read
1523 GraphReader& nodes(const std::string& caption) {
1524 _nodes_caption = caption;
1528 /// \brief Set \c \@edges section to be read
1530 /// Set \c \@edges section to be read
1531 GraphReader& edges(const std::string& caption) {
1532 _edges_caption = caption;
1536 /// \brief Set \c \@attributes section to be read
1538 /// Set \c \@attributes section to be read
1539 GraphReader& attributes(const std::string& caption) {
1540 _attributes_caption = caption;
1546 /// \name Section readers
1549 /// \brief Add a section processor with line oriented reading
1551 /// In the \e LGF file extra sections can be placed, which contain
1552 /// any data in arbitrary format. These sections can be read with
1553 /// this function line by line. The first parameter is the type
1554 /// descriptor of the section, the second is a functor, which
1555 /// takes just one \c std::string parameter. At the reading
1556 /// process, each line of the section will be given to the functor
1557 /// object. However, the empty lines and the comment lines are
1558 /// filtered out, and the leading whitespaces are stipped from
1559 /// each processed string.
1561 /// For example let's see a section, which contain several
1562 /// integers, which should be inserted into a vector.
1570 /// The functor is implemented as an struct:
1572 /// struct NumberSection {
1573 /// std::vector<int>& _data;
1574 /// NumberSection(std::vector<int>& data) : _data(data) {}
1575 /// void operator()(const std::string& line) {
1576 /// std::istringstream ls(line);
1578 /// while (ls >> value) _data.push_back(value);
1584 /// reader.sectionLines("numbers", NumberSection(vec));
1586 template <typename Functor>
1587 GraphReader& sectionLines(const std::string& type, Functor functor) {
1588 LEMON_ASSERT(!type.empty(), "Type is not empty.");
1589 LEMON_ASSERT(_sections.find(type) == _sections.end(),
1590 "Multiple reading of section.");
1591 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
1592 type != "attributes", "Multiple reading of section.");
1593 _sections.insert(std::make_pair(type,
1594 new _reader_bits::LineSection<Functor>(functor)));
1599 /// \brief Add a section processor with stream oriented reading
1601 /// In the \e LGF file extra sections can be placed, which contain
1602 /// any data in arbitrary format. These sections can be read
1603 /// directly with this function. The first parameter is the type
1604 /// of the section, the second is a functor, which takes an \c
1605 /// std::istream& and an int& parameter, the latter regard to the
1606 /// line number of stream. The functor can read the input while
1607 /// the section go on, and the line number should be modified
1609 template <typename Functor>
1610 GraphReader& sectionStream(const std::string& type, Functor functor) {
1611 LEMON_ASSERT(!type.empty(), "Type is not empty.");
1612 LEMON_ASSERT(_sections.find(type) == _sections.end(),
1613 "Multiple reading of section.");
1614 LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
1615 type != "attributes", "Multiple reading of section.");
1616 _sections.insert(std::make_pair(type,
1617 new _reader_bits::StreamSection<Functor>(functor)));
1623 /// \name Using previously constructed node or edge set
1626 /// \brief Use previously constructed node set
1628 /// Use previously constructed node set, and specify the node
1630 template <typename Map>
1631 GraphReader& useNodes(const Map& map) {
1632 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1633 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1635 _writer_bits::DefaultConverter<typename Map::Value> converter;
1636 for (NodeIt n(_graph); n != INVALID; ++n) {
1637 _node_index.insert(std::make_pair(converter(map[n]), n));
1642 /// \brief Use previously constructed node set
1644 /// Use previously constructed node set, and specify the node
1645 /// label map and a functor which converts the label map values to
1647 template <typename Map, typename Converter>
1648 GraphReader& useNodes(const Map& map,
1649 const Converter& converter = Converter()) {
1650 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1651 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1653 for (NodeIt n(_graph); n != INVALID; ++n) {
1654 _node_index.insert(std::make_pair(converter(map[n]), n));
1659 /// \brief Use previously constructed edge set
1661 /// Use previously constructed edge set, and specify the edge
1663 template <typename Map>
1664 GraphReader& useEdges(const Map& map) {
1665 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1666 LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1668 _writer_bits::DefaultConverter<typename Map::Value> converter;
1669 for (EdgeIt a(_graph); a != INVALID; ++a) {
1670 _edge_index.insert(std::make_pair(converter(map[a]), a));
1675 /// \brief Use previously constructed edge set
1677 /// Use previously constructed edge set, and specify the edge
1678 /// label map and a functor which converts the label map values to
1680 template <typename Map, typename Converter>
1681 GraphReader& useEdges(const Map& map,
1682 const Converter& converter = Converter()) {
1683 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1684 LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1686 for (EdgeIt a(_graph); a != INVALID; ++a) {
1687 _edge_index.insert(std::make_pair(converter(map[a]), a));
1698 while(++line_num, std::getline(*_is, str)) {
1699 line.clear(); line.str(str);
1701 if (line >> std::ws >> c && c != '#') {
1709 bool readSuccess() {
1710 return static_cast<bool>(*_is);
1713 void skipSection() {
1715 while (readSuccess() && line >> c && c != '@') {
1723 std::vector<int> map_index(_node_maps.size());
1724 int map_num, label_index;
1727 throw DataFormatError("Cannot find map captions");
1730 std::map<std::string, int> maps;
1734 while (_reader_bits::readToken(line, map)) {
1735 if (maps.find(map) != maps.end()) {
1736 std::ostringstream msg;
1737 msg << "Multiple occurence of node map: " << map;
1738 throw DataFormatError(msg.str().c_str());
1740 maps.insert(std::make_pair(map, index));
1744 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
1745 std::map<std::string, int>::iterator jt =
1746 maps.find(_node_maps[i].first);
1747 if (jt == maps.end()) {
1748 std::ostringstream msg;
1749 msg << "Map not found in file: " << _node_maps[i].first;
1750 throw DataFormatError(msg.str().c_str());
1752 map_index[i] = jt->second;
1756 std::map<std::string, int>::iterator jt = maps.find("label");
1757 if (jt == maps.end())
1758 throw DataFormatError("Label map not found in file");
1759 label_index = jt->second;
1761 map_num = maps.size();
1765 while (readLine() && line >> c && c != '@') {
1768 std::vector<std::string> tokens(map_num);
1769 for (int i = 0; i < map_num; ++i) {
1770 if (!_reader_bits::readToken(line, tokens[i])) {
1771 std::ostringstream msg;
1772 msg << "Column not found (" << i + 1 << ")";
1773 throw DataFormatError(msg.str().c_str());
1776 if (line >> std::ws >> c)
1777 throw DataFormatError("Extra character on the end of line");
1781 n = _graph.addNode();
1782 _node_index.insert(std::make_pair(tokens[label_index], n));
1784 typename std::map<std::string, Node>::iterator it =
1785 _node_index.find(tokens[label_index]);
1786 if (it == _node_index.end()) {
1787 std::ostringstream msg;
1788 msg << "Node with label not found: " << tokens[label_index];
1789 throw DataFormatError(msg.str().c_str());
1794 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
1795 _node_maps[i].second->set(n, tokens[map_index[i]]);
1799 if (readSuccess()) {
1806 std::vector<int> map_index(_edge_maps.size());
1807 int map_num, label_index;
1810 throw DataFormatError("Cannot find map captions");
1813 std::map<std::string, int> maps;
1817 while (_reader_bits::readToken(line, map)) {
1818 if (maps.find(map) != maps.end()) {
1819 std::ostringstream msg;
1820 msg << "Multiple occurence of edge map: " << map;
1821 throw DataFormatError(msg.str().c_str());
1823 maps.insert(std::make_pair(map, index));
1827 for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1828 std::map<std::string, int>::iterator jt =
1829 maps.find(_edge_maps[i].first);
1830 if (jt == maps.end()) {
1831 std::ostringstream msg;
1832 msg << "Map not found in file: " << _edge_maps[i].first;
1833 throw DataFormatError(msg.str().c_str());
1835 map_index[i] = jt->second;
1839 std::map<std::string, int>::iterator jt = maps.find("label");
1840 if (jt == maps.end())
1841 throw DataFormatError("Label map not found in file");
1842 label_index = jt->second;
1844 map_num = maps.size();
1848 while (readLine() && line >> c && c != '@') {
1851 std::string source_token;
1852 std::string target_token;
1854 if (!_reader_bits::readToken(line, source_token))
1855 throw DataFormatError("Source not found");
1857 if (!_reader_bits::readToken(line, target_token))
1858 throw DataFormatError("Source not found");
1860 std::vector<std::string> tokens(map_num);
1861 for (int i = 0; i < map_num; ++i) {
1862 if (!_reader_bits::readToken(line, tokens[i])) {
1863 std::ostringstream msg;
1864 msg << "Column not found (" << i + 1 << ")";
1865 throw DataFormatError(msg.str().c_str());
1868 if (line >> std::ws >> c)
1869 throw DataFormatError("Extra character on the end of line");
1874 typename NodeIndex::iterator it;
1876 it = _node_index.find(source_token);
1877 if (it == _node_index.end()) {
1878 std::ostringstream msg;
1879 msg << "Item not found: " << source_token;
1880 throw DataFormatError(msg.str().c_str());
1882 Node source = it->second;
1884 it = _node_index.find(target_token);
1885 if (it == _node_index.end()) {
1886 std::ostringstream msg;
1887 msg << "Item not found: " << target_token;
1888 throw DataFormatError(msg.str().c_str());
1890 Node target = it->second;
1892 e = _graph.addEdge(source, target);
1893 _edge_index.insert(std::make_pair(tokens[label_index], e));
1895 typename std::map<std::string, Edge>::iterator it =
1896 _edge_index.find(tokens[label_index]);
1897 if (it == _edge_index.end()) {
1898 std::ostringstream msg;
1899 msg << "Edge with label not found: " << tokens[label_index];
1900 throw DataFormatError(msg.str().c_str());
1905 for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1906 _edge_maps[i].second->set(e, tokens[map_index[i]]);
1910 if (readSuccess()) {
1915 void readAttributes() {
1917 std::set<std::string> read_attr;
1920 while (readLine() && line >> c && c != '@') {
1923 std::string attr, token;
1924 if (!_reader_bits::readToken(line, attr))
1925 throw DataFormatError("Attribute name not found");
1926 if (!_reader_bits::readToken(line, token))
1927 throw DataFormatError("Attribute value not found");
1929 throw DataFormatError("Extra character on the end of line");
1932 std::set<std::string>::iterator it = read_attr.find(attr);
1933 if (it != read_attr.end()) {
1934 std::ostringstream msg;
1935 msg << "Multiple occurence of attribute " << attr;
1936 throw DataFormatError(msg.str().c_str());
1938 read_attr.insert(attr);
1942 typename Attributes::iterator it = _attributes.lower_bound(attr);
1943 while (it != _attributes.end() && it->first == attr) {
1944 it->second->set(token);
1950 if (readSuccess()) {
1953 for (typename Attributes::iterator it = _attributes.begin();
1954 it != _attributes.end(); ++it) {
1955 if (read_attr.find(it->first) == read_attr.end()) {
1956 std::ostringstream msg;
1957 msg << "Attribute not found in file: " << it->first;
1958 throw DataFormatError(msg.str().c_str());
1965 /// \name Execution of the reader
1968 /// \brief Start the batch processing
1970 /// This function starts the batch processing
1973 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1975 bool nodes_done = false;
1976 bool edges_done = false;
1977 bool attributes_done = false;
1978 std::set<std::string> extra_sections;
1984 while (readSuccess()) {
1987 std::string section, caption;
1989 _reader_bits::readToken(line, section);
1990 _reader_bits::readToken(line, caption);
1993 throw DataFormatError("Extra character on the end of line");
1995 if (section == "nodes" && !nodes_done) {
1996 if (_nodes_caption.empty() || _nodes_caption == caption) {
2000 } else if ((section == "edges" || section == "arcs") &&
2002 if (_edges_caption.empty() || _edges_caption == caption) {
2006 } else if (section == "attributes" && !attributes_done) {
2007 if (_attributes_caption.empty() || _attributes_caption == caption) {
2009 attributes_done = true;
2012 if (extra_sections.find(section) != extra_sections.end()) {
2013 std::ostringstream msg;
2014 msg << "Multiple occurence of section " << section;
2015 throw DataFormatError(msg.str().c_str());
2017 Sections::iterator it = _sections.find(section);
2018 if (it != _sections.end()) {
2019 extra_sections.insert(section);
2020 it->second->process(*_is, line_num);
2025 } catch (DataFormatError& error) {
2026 error.line(line_num);
2032 throw DataFormatError("Section @nodes not found");
2036 throw DataFormatError("Section @edges not found");
2039 if (!attributes_done && !_attributes.empty()) {
2040 throw DataFormatError("Section @attributes not found");
2049 /// \relates GraphReader
2050 template <typename Graph>
2051 GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
2052 GraphReader<Graph> tmp(is, graph);
2056 /// \relates GraphReader
2057 template <typename Graph>
2058 GraphReader<Graph> graphReader(const std::string& fn,
2060 GraphReader<Graph> tmp(fn, graph);
2064 /// \relates GraphReader
2065 template <typename Graph>
2066 GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
2067 GraphReader<Graph> tmp(fn, graph);
2071 /// \ingroup lemon_io
2073 /// \brief Reader for the contents of the \ref lgf-format "LGF" file
2075 /// This class can be used to read the sections, the map names and
2076 /// the attributes from a file. Usually, the Lemon programs know
2077 /// that, which type of graph, which maps and which attributes
2078 /// should be read from a file, but in general tools (like glemon)
2079 /// the contents of an LGF file should be guessed somehow. This class
2080 /// reads the graph and stores the appropriate information for
2081 /// reading the graph.
2083 ///\code LgfContents contents("graph.lgf");
2086 /// // does it contain any node section and arc section
2087 /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
2088 /// std::cerr << "Failure, cannot find graph" << std::endl;
2091 /// std::cout << "The name of the default node section : "
2092 /// << contents.nodeSection(0) << std::endl;
2093 /// std::cout << "The number of the arc maps : "
2094 /// << contents.arcMaps(0).size() << std::endl;
2095 /// std::cout << "The name of second arc map : "
2096 /// << contents.arcMaps(0)[1] << std::endl;
2104 std::vector<std::string> _node_sections;
2105 std::vector<std::string> _edge_sections;
2106 std::vector<std::string> _attribute_sections;
2107 std::vector<std::string> _extra_sections;
2109 std::vector<bool> _arc_sections;
2111 std::vector<std::vector<std::string> > _node_maps;
2112 std::vector<std::vector<std::string> > _edge_maps;
2114 std::vector<std::vector<std::string> > _attributes;
2118 std::istringstream line;
2122 /// \brief Constructor
2124 /// Construct an \e LGF contents reader, which reads from the given
2126 LgfContents(std::istream& is)
2127 : _is(&is), local_is(false) {}
2129 /// \brief Constructor
2131 /// Construct an \e LGF contents reader, which reads from the given
2133 LgfContents(const std::string& fn)
2134 : _is(new std::ifstream(fn.c_str())), local_is(true) {}
2136 /// \brief Constructor
2138 /// Construct an \e LGF contents reader, which reads from the given
2140 LgfContents(const char* fn)
2141 : _is(new std::ifstream(fn)), local_is(true) {}
2143 /// \brief Copy constructor
2145 /// The copy constructor transfers all data from the other reader,
2146 /// therefore the copied reader will not be usable more.
2147 LgfContents(LgfContents& other)
2148 : _is(other._is), local_is(other.local_is) {
2151 other.local_is = false;
2153 _node_sections.swap(other._node_sections);
2154 _edge_sections.swap(other._edge_sections);
2155 _attribute_sections.swap(other._attribute_sections);
2156 _extra_sections.swap(other._extra_sections);
2158 _arc_sections.swap(other._arc_sections);
2160 _node_maps.swap(other._node_maps);
2161 _edge_maps.swap(other._edge_maps);
2162 _attributes.swap(other._attributes);
2165 /// \brief Destructor
2167 if (local_is) delete _is;
2171 /// \name Node sections
2174 /// \brief Gives back the number of node sections in the file.
2176 /// Gives back the number of node sections in the file.
2177 int nodeSectionNum() const {
2178 return _node_sections.size();
2181 /// \brief Returns the section name at the given position.
2183 /// Returns the section name at the given position.
2184 const std::string& nodeSection(int i) const {
2185 return _node_sections[i];
2188 /// \brief Gives back the node maps for the given section.
2190 /// Gives back the node maps for the given section.
2191 const std::vector<std::string>& nodeMapNames(int i) const {
2192 return _node_maps[i];
2197 /// \name Arc/Edge sections
2200 /// \brief Gives back the number of arc/edge sections in the file.
2202 /// Gives back the number of arc/edge sections in the file.
2203 /// \note It is synonym of \c edgeSectionNum().
2204 int arcSectionNum() const {
2205 return _edge_sections.size();
2208 /// \brief Returns the section name at the given position.
2210 /// Returns the section name at the given position.
2211 /// \note It is synonym of \c edgeSection().
2212 const std::string& arcSection(int i) const {
2213 return _edge_sections[i];
2216 /// \brief Gives back the arc/edge maps for the given section.
2218 /// Gives back the arc/edge maps for the given section.
2219 /// \note It is synonym of \c edgeMapNames().
2220 const std::vector<std::string>& arcMapNames(int i) const {
2221 return _edge_maps[i];
2229 /// \brief Gives back the number of arc/edge sections in the file.
2231 /// Gives back the number of arc/edge sections in the file.
2232 /// \note It is synonym of \c arcSectionNum().
2233 int edgeSectionNum() const {
2234 return _edge_sections.size();
2237 /// \brief Returns the section name at the given position.
2239 /// Returns the section name at the given position.
2240 /// \note It is synonym of \c arcSection().
2241 const std::string& edgeSection(int i) const {
2242 return _edge_sections[i];
2245 /// \brief Gives back the edge maps for the given section.
2247 /// Gives back the edge maps for the given section.
2248 /// \note It is synonym of \c arcMapNames().
2249 const std::vector<std::string>& edgeMapNames(int i) const {
2250 return _edge_maps[i];
2255 /// \name Attribute sections
2258 /// \brief Gives back the number of attribute sections in the file.
2260 /// Gives back the number of attribute sections in the file.
2261 int attributeSectionNum() const {
2262 return _attribute_sections.size();
2265 /// \brief Returns the section name at the given position.
2267 /// Returns the section name at the given position.
2268 const std::string& attributeSectionNames(int i) const {
2269 return _attribute_sections[i];
2272 /// \brief Gives back the attributes for the given section.
2274 /// Gives back the attributes for the given section.
2275 const std::vector<std::string>& attributes(int i) const {
2276 return _attributes[i];
2281 /// \name Extra sections
2284 /// \brief Gives back the number of extra sections in the file.
2286 /// Gives back the number of extra sections in the file.
2287 int extraSectionNum() const {
2288 return _extra_sections.size();
2291 /// \brief Returns the extra section type at the given position.
2293 /// Returns the section type at the given position.
2294 const std::string& extraSection(int i) const {
2295 return _extra_sections[i];
2304 while(++line_num, std::getline(*_is, str)) {
2305 line.clear(); line.str(str);
2307 if (line >> std::ws >> c && c != '#') {
2315 bool readSuccess() {
2316 return static_cast<bool>(*_is);
2319 void skipSection() {
2321 while (readSuccess() && line >> c && c != '@') {
2327 void readMaps(std::vector<std::string>& maps) {
2329 throw DataFormatError("Cannot find map captions");
2331 while (_reader_bits::readToken(line, map)) {
2332 maps.push_back(map);
2336 void readAttributes(std::vector<std::string>& attrs) {
2339 while (readSuccess() && line >> c && c != '@') {
2342 _reader_bits::readToken(line, attr);
2343 attrs.push_back(attr);
2351 /// \name Execution of the contents reader
2354 /// \brief Start the reading
2356 /// This function starts the reading
2362 while (readSuccess()) {
2367 std::string section, caption;
2368 _reader_bits::readToken(line, section);
2369 _reader_bits::readToken(line, caption);
2371 if (section == "nodes") {
2372 _node_sections.push_back(caption);
2373 _node_maps.push_back(std::vector<std::string>());
2374 readMaps(_node_maps.back());
2375 readLine(); skipSection();
2376 } else if (section == "arcs" || section == "edges") {
2377 _edge_sections.push_back(caption);
2378 _arc_sections.push_back(section == "arcs");
2379 _edge_maps.push_back(std::vector<std::string>());
2380 readMaps(_edge_maps.back());
2381 readLine(); skipSection();
2382 } else if (section == "attributes") {
2383 _attribute_sections.push_back(caption);
2384 _attributes.push_back(std::vector<std::string>());
2385 readAttributes(_attributes.back());
2387 _extra_sections.push_back(section);
2388 readLine(); skipSection();