1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2011
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 \ref lgf-format "LEMON Graph Format" reader.
24 #ifndef LEMON_LGF_READER_H
25 #define LEMON_LGF_READER_H
34 #include <lemon/core.h>
36 #include <lemon/lgf_writer.h>
38 #include <lemon/concept_check.h>
39 #include <lemon/concepts/maps.h>
43 namespace _reader_bits {
45 template <typename Value>
46 struct DefaultConverter {
47 Value operator()(const std::string& str) {
48 std::istringstream is(str);
51 throw FormatError("Cannot read token");
55 if (is >> std::ws >> c) {
56 throw FormatError("Remaining characters in token");
63 struct DefaultConverter<std::string> {
64 std::string operator()(const std::string& str) {
69 template <typename _Item>
70 class MapStorageBase {
76 virtual ~MapStorageBase() {}
78 virtual void set(const Item& item, const std::string& value) = 0;
82 template <typename _Item, typename _Map,
83 typename _Converter = DefaultConverter<typename _Map::Value> >
84 class MapStorage : public MapStorageBase<_Item> {
87 typedef _Converter Converter;
95 MapStorage(Map& map, const Converter& converter = Converter())
96 : _map(map), _converter(converter) {}
97 virtual ~MapStorage() {}
99 virtual void set(const Item& item ,const std::string& value) {
100 _map.set(item, _converter(value));
104 template <typename _GR, bool _dir, typename _Map,
105 typename _Converter = DefaultConverter<typename _Map::Value> >
106 class GraphArcMapStorage : public MapStorageBase<typename _GR::Edge> {
109 typedef _Converter Converter;
111 typedef typename GR::Edge Item;
112 static const bool dir = _dir;
117 Converter _converter;
120 GraphArcMapStorage(const GR& graph, Map& map,
121 const Converter& converter = Converter())
122 : _graph(graph), _map(map), _converter(converter) {}
123 virtual ~GraphArcMapStorage() {}
125 virtual void set(const Item& item ,const std::string& value) {
126 _map.set(_graph.direct(item, dir), _converter(value));
130 class ValueStorageBase {
132 ValueStorageBase() {}
133 virtual ~ValueStorageBase() {}
135 virtual void set(const std::string&) = 0;
138 template <typename _Value, typename _Converter = DefaultConverter<_Value> >
139 class ValueStorage : public ValueStorageBase {
141 typedef _Value Value;
142 typedef _Converter Converter;
146 Converter _converter;
149 ValueStorage(Value& value, const Converter& converter = Converter())
150 : _value(value), _converter(converter) {}
152 virtual void set(const std::string& value) {
153 _value = _converter(value);
157 template <typename Value>
158 struct MapLookUpConverter {
159 const std::map<std::string, Value>& _map;
161 MapLookUpConverter(const std::map<std::string, Value>& map)
164 Value operator()(const std::string& str) {
165 typename std::map<std::string, Value>::const_iterator it =
167 if (it == _map.end()) {
168 std::ostringstream msg;
169 msg << "Item not found: " << str;
170 throw FormatError(msg.str());
176 template <typename GR>
177 struct GraphArcLookUpConverter {
179 const std::map<std::string, typename GR::Edge>& _map;
181 GraphArcLookUpConverter(const GR& graph,
182 const std::map<std::string,
183 typename GR::Edge>& map)
184 : _graph(graph), _map(map) {}
186 typename GR::Arc operator()(const std::string& str) {
187 if (str.empty() || (str[0] != '+' && str[0] != '-')) {
188 throw FormatError("Item must start with '+' or '-'");
190 typename std::map<std::string, typename GR::Edge>
191 ::const_iterator it = _map.find(str.substr(1));
192 if (it == _map.end()) {
193 throw FormatError("Item not found");
195 return _graph.direct(it->second, str[0] == '+');
199 inline bool isWhiteSpace(char c) {
200 return c == ' ' || c == '\t' || c == '\v' ||
201 c == '\n' || c == '\r' || c == '\f';
204 inline bool isOct(char c) {
205 return '0' <= c && c <='7';
208 inline int valueOct(char c) {
209 LEMON_ASSERT(isOct(c), "The character is not octal.");
213 inline bool isHex(char c) {
214 return ('0' <= c && c <= '9') ||
215 ('a' <= c && c <= 'z') ||
216 ('A' <= c && c <= 'Z');
219 inline int valueHex(char c) {
220 LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
221 if ('0' <= c && c <= '9') return c - '0';
222 if ('a' <= c && c <= 'z') return c - 'a' + 10;
226 inline bool isIdentifierFirstChar(char c) {
227 return ('a' <= c && c <= 'z') ||
228 ('A' <= c && c <= 'Z') || c == '_';
231 inline bool isIdentifierChar(char c) {
232 return isIdentifierFirstChar(c) ||
233 ('0' <= c && c <= '9');
236 inline char readEscape(std::istream& is) {
239 throw FormatError("Escape format error");
267 if (!is.get(c) || !isHex(c))
268 throw FormatError("Escape format error");
269 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
270 else code = code * 16 + valueHex(c);
277 throw FormatError("Escape format error");
278 else if (code = valueOct(c), !is.get(c) || !isOct(c))
280 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
282 else code = code * 8 + valueOct(c);
288 inline std::istream& readToken(std::istream& is, std::string& str) {
289 std::ostringstream os;
298 while (is.get(c) && c != '\"') {
304 throw FormatError("Quoted format error");
307 while (is.get(c) && !isWhiteSpace(c)) {
324 virtual ~Section() {}
325 virtual void process(std::istream& is, int& line_num) = 0;
328 template <typename Functor>
329 class LineSection : public Section {
336 LineSection(const Functor& functor) : _functor(functor) {}
337 virtual ~LineSection() {}
339 virtual void process(std::istream& is, int& line_num) {
342 while (is.get(c) && c != '@') {
345 } else if (c == '#') {
348 } else if (!isWhiteSpace(c)) {
355 if (is) is.putback(c);
356 else if (is.eof()) is.clear();
360 template <typename Functor>
361 class StreamSection : public Section {
368 StreamSection(const Functor& functor) : _functor(functor) {}
369 virtual ~StreamSection() {}
371 virtual void process(std::istream& is, int& line_num) {
372 _functor(is, line_num);
375 while (is.get(c) && c != '@') {
378 } else if (!isWhiteSpace(c)) {
383 if (is) is.putback(c);
384 else if (is.eof()) is.clear();
390 template <typename DGR>
393 template <typename TDGR>
394 DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is = std::cin);
395 template <typename TDGR>
396 DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn);
397 template <typename TDGR>
398 DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
400 /// \ingroup lemon_io
402 /// \brief \ref lgf-format "LGF" reader for directed graphs
404 /// This utility reads an \ref lgf-format "LGF" file.
406 /// The reading method does a batch processing. The user creates a
407 /// reader object, then various reading rules can be added to the
408 /// reader, and eventually the reading is executed with the \c run()
409 /// member function. A map reading rule can be added to the reader
410 /// with the \c nodeMap() or \c arcMap() members. An optional
411 /// converter parameter can also be added as a standard functor
412 /// converting from \c std::string to the value type of the map. If it
413 /// is set, it will determine how the tokens in the file should be
414 /// converted to the value type of the map. If the functor is not set,
415 /// then a default conversion will be used. One map can be read into
416 /// multiple map objects at the same time. The \c attribute(), \c
417 /// node() and \c arc() functions are used to add attribute reading
421 /// DigraphReader<DGR>(digraph, std::cin).
422 /// nodeMap("coordinates", coord_map).
423 /// arcMap("capacity", cap_map).
424 /// node("source", src).
425 /// node("target", trg).
426 /// attribute("caption", caption).
430 /// By default, the reader uses the first section in the file of the
431 /// proper type. If a section has an optional name, then it can be
432 /// selected for reading by giving an optional name parameter to the
433 /// \c nodes(), \c arcs() or \c attributes() functions.
435 /// The \c useNodes() and \c useArcs() functions are used to tell the reader
436 /// that the nodes or arcs should not be constructed (added to the
437 /// graph) during the reading, but instead the label map of the items
438 /// are given as a parameter of these functions. An
439 /// application of these functions is multipass reading, which is
440 /// important if two \c \@arcs sections must be read from the
441 /// file. In this case the first phase would read the node set and one
442 /// of the arc sets, while the second phase would read the second arc
443 /// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
444 /// The previously read label node map should be passed to the \c
445 /// useNodes() functions. Another application of multipass reading when
446 /// paths are given as a node map or an arc map.
447 /// It is impossible to read this in
448 /// a single pass, because the arcs are not constructed when the node
450 template <typename DGR>
451 class DigraphReader {
458 TEMPLATE_DIGRAPH_TYPEDEFS(DGR);
462 std::string _filename;
466 std::string _nodes_caption;
467 std::string _arcs_caption;
468 std::string _attributes_caption;
470 typedef std::map<std::string, Node> NodeIndex;
471 NodeIndex _node_index;
472 typedef std::map<std::string, Arc> ArcIndex;
475 typedef std::vector<std::pair<std::string,
476 _reader_bits::MapStorageBase<Node>*> > NodeMaps;
479 typedef std::vector<std::pair<std::string,
480 _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
483 typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
485 Attributes _attributes;
494 std::istringstream line;
498 /// \brief Constructor
500 /// Construct a directed graph reader, which reads from the given
502 DigraphReader(DGR& digraph, std::istream& is = std::cin)
503 : _is(&is), local_is(false), _digraph(digraph),
504 _use_nodes(false), _use_arcs(false),
505 _skip_nodes(false), _skip_arcs(false) {}
507 /// \brief Constructor
509 /// Construct a directed graph reader, which reads from the given
511 DigraphReader(DGR& digraph, const std::string& fn)
512 : _is(new std::ifstream(fn.c_str())), local_is(true),
513 _filename(fn), _digraph(digraph),
514 _use_nodes(false), _use_arcs(false),
515 _skip_nodes(false), _skip_arcs(false) {
518 throw IoError("Cannot open file", fn);
522 /// \brief Constructor
524 /// Construct a directed graph reader, which reads from the given
526 DigraphReader(DGR& digraph, const char* fn)
527 : _is(new std::ifstream(fn)), local_is(true),
528 _filename(fn), _digraph(digraph),
529 _use_nodes(false), _use_arcs(false),
530 _skip_nodes(false), _skip_arcs(false) {
533 throw IoError("Cannot open file", fn);
537 /// \brief Destructor
539 for (typename NodeMaps::iterator it = _node_maps.begin();
540 it != _node_maps.end(); ++it) {
544 for (typename ArcMaps::iterator it = _arc_maps.begin();
545 it != _arc_maps.end(); ++it) {
549 for (typename Attributes::iterator it = _attributes.begin();
550 it != _attributes.end(); ++it) {
562 template <typename TDGR>
563 friend DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is);
564 template <typename TDGR>
565 friend DigraphReader<TDGR> digraphReader(TDGR& digraph,
566 const std::string& fn);
567 template <typename TDGR>
568 friend DigraphReader<TDGR> digraphReader(TDGR& digraph, const char *fn);
570 DigraphReader(DigraphReader& other)
571 : _is(other._is), local_is(other.local_is), _digraph(other._digraph),
572 _use_nodes(other._use_nodes), _use_arcs(other._use_arcs),
573 _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) {
576 other.local_is = false;
578 _node_index.swap(other._node_index);
579 _arc_index.swap(other._arc_index);
581 _node_maps.swap(other._node_maps);
582 _arc_maps.swap(other._arc_maps);
583 _attributes.swap(other._attributes);
585 _nodes_caption = other._nodes_caption;
586 _arcs_caption = other._arcs_caption;
587 _attributes_caption = other._attributes_caption;
591 DigraphReader& operator=(const DigraphReader&);
595 /// \name Reading Rules
598 /// \brief Node map reading rule
600 /// Add a node map reading rule to the reader.
601 template <typename Map>
602 DigraphReader& nodeMap(const std::string& caption, Map& map) {
603 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
604 _reader_bits::MapStorageBase<Node>* storage =
605 new _reader_bits::MapStorage<Node, Map>(map);
606 _node_maps.push_back(std::make_pair(caption, storage));
610 /// \brief Node map reading rule
612 /// Add a node map reading rule with specialized converter to the
614 template <typename Map, typename Converter>
615 DigraphReader& nodeMap(const std::string& caption, Map& map,
616 const Converter& converter = Converter()) {
617 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
618 _reader_bits::MapStorageBase<Node>* storage =
619 new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
620 _node_maps.push_back(std::make_pair(caption, storage));
624 /// \brief Arc map reading rule
626 /// Add an arc map reading rule to the reader.
627 template <typename Map>
628 DigraphReader& arcMap(const std::string& caption, Map& map) {
629 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
630 _reader_bits::MapStorageBase<Arc>* storage =
631 new _reader_bits::MapStorage<Arc, Map>(map);
632 _arc_maps.push_back(std::make_pair(caption, storage));
636 /// \brief Arc map reading rule
638 /// Add an arc map reading rule with specialized converter to the
640 template <typename Map, typename Converter>
641 DigraphReader& arcMap(const std::string& caption, Map& map,
642 const Converter& converter = Converter()) {
643 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
644 _reader_bits::MapStorageBase<Arc>* storage =
645 new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
646 _arc_maps.push_back(std::make_pair(caption, storage));
650 /// \brief Attribute reading rule
652 /// Add an attribute reading rule to the reader.
653 template <typename Value>
654 DigraphReader& attribute(const std::string& caption, Value& value) {
655 _reader_bits::ValueStorageBase* storage =
656 new _reader_bits::ValueStorage<Value>(value);
657 _attributes.insert(std::make_pair(caption, storage));
661 /// \brief Attribute reading rule
663 /// Add an attribute reading rule with specialized converter to the
665 template <typename Value, typename Converter>
666 DigraphReader& attribute(const std::string& caption, Value& value,
667 const Converter& converter = Converter()) {
668 _reader_bits::ValueStorageBase* storage =
669 new _reader_bits::ValueStorage<Value, Converter>(value, converter);
670 _attributes.insert(std::make_pair(caption, storage));
674 /// \brief Node reading rule
676 /// Add a node reading rule to reader.
677 DigraphReader& node(const std::string& caption, Node& node) {
678 typedef _reader_bits::MapLookUpConverter<Node> Converter;
679 Converter converter(_node_index);
680 _reader_bits::ValueStorageBase* storage =
681 new _reader_bits::ValueStorage<Node, Converter>(node, converter);
682 _attributes.insert(std::make_pair(caption, storage));
686 /// \brief Arc reading rule
688 /// Add an arc reading rule to reader.
689 DigraphReader& arc(const std::string& caption, Arc& arc) {
690 typedef _reader_bits::MapLookUpConverter<Arc> Converter;
691 Converter converter(_arc_index);
692 _reader_bits::ValueStorageBase* storage =
693 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
694 _attributes.insert(std::make_pair(caption, storage));
700 /// \name Select Section by Name
703 /// \brief Set \c \@nodes section to be read
705 /// Set \c \@nodes section to be read
706 DigraphReader& nodes(const std::string& caption) {
707 _nodes_caption = caption;
711 /// \brief Set \c \@arcs section to be read
713 /// Set \c \@arcs section to be read
714 DigraphReader& arcs(const std::string& caption) {
715 _arcs_caption = caption;
719 /// \brief Set \c \@attributes section to be read
721 /// Set \c \@attributes section to be read
722 DigraphReader& attributes(const std::string& caption) {
723 _attributes_caption = caption;
729 /// \name Using Previously Constructed Node or Arc Set
732 /// \brief Use previously constructed node set
734 /// Use previously constructed node set, and specify the node
736 template <typename Map>
737 DigraphReader& useNodes(const Map& map) {
738 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
739 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
741 _writer_bits::DefaultConverter<typename Map::Value> converter;
742 for (NodeIt n(_digraph); n != INVALID; ++n) {
743 _node_index.insert(std::make_pair(converter(map[n]), n));
748 /// \brief Use previously constructed node set
750 /// Use previously constructed node set, and specify the node
751 /// label map and a functor which converts the label map values to
753 template <typename Map, typename Converter>
754 DigraphReader& useNodes(const Map& map,
755 const Converter& converter = Converter()) {
756 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
757 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
759 for (NodeIt n(_digraph); n != INVALID; ++n) {
760 _node_index.insert(std::make_pair(converter(map[n]), n));
765 /// \brief Use previously constructed arc set
767 /// Use previously constructed arc set, and specify the arc
769 template <typename Map>
770 DigraphReader& useArcs(const Map& map) {
771 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
772 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
774 _writer_bits::DefaultConverter<typename Map::Value> converter;
775 for (ArcIt a(_digraph); a != INVALID; ++a) {
776 _arc_index.insert(std::make_pair(converter(map[a]), a));
781 /// \brief Use previously constructed arc set
783 /// Use previously constructed arc set, and specify the arc
784 /// label map and a functor which converts the label map values to
786 template <typename Map, typename Converter>
787 DigraphReader& useArcs(const Map& map,
788 const Converter& converter = Converter()) {
789 checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
790 LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
792 for (ArcIt a(_digraph); a != INVALID; ++a) {
793 _arc_index.insert(std::make_pair(converter(map[a]), a));
798 /// \brief Skips the reading of node section
800 /// Omit the reading of the node section. This implies that each node
801 /// map reading rule will be abandoned, and the nodes of the graph
802 /// will not be constructed, which usually cause that the arc set
803 /// could not be read due to lack of node name resolving.
804 /// Therefore \c skipArcs() function should also be used, or
805 /// \c useNodes() should be used to specify the label of the nodes.
806 DigraphReader& skipNodes() {
807 LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
812 /// \brief Skips the reading of arc section
814 /// Omit the reading of the arc section. This implies that each arc
815 /// map reading rule will be abandoned, and the arcs of the graph
816 /// will not be constructed.
817 DigraphReader& skipArcs() {
818 LEMON_ASSERT(!_skip_arcs, "Skip arcs already set");
829 while(++line_num, std::getline(*_is, str)) {
830 line.clear(); line.str(str);
832 if (line >> std::ws >> c && c != '#') {
841 return static_cast<bool>(*_is);
846 while (readSuccess() && line >> c && c != '@') {
856 std::vector<int> map_index(_node_maps.size());
857 int map_num, label_index;
860 if (!readLine() || !(line >> c) || c == '@') {
861 if (readSuccess() && line) line.putback(c);
862 if (!_node_maps.empty())
863 throw FormatError("Cannot find map names");
869 std::map<std::string, int> maps;
873 while (_reader_bits::readToken(line, map)) {
874 if (maps.find(map) != maps.end()) {
875 std::ostringstream msg;
876 msg << "Multiple occurence of node map: " << map;
877 throw FormatError(msg.str());
879 maps.insert(std::make_pair(map, index));
883 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
884 std::map<std::string, int>::iterator jt =
885 maps.find(_node_maps[i].first);
886 if (jt == maps.end()) {
887 std::ostringstream msg;
888 msg << "Map not found: " << _node_maps[i].first;
889 throw FormatError(msg.str());
891 map_index[i] = jt->second;
895 std::map<std::string, int>::iterator jt = maps.find("label");
896 if (jt != maps.end()) {
897 label_index = jt->second;
902 map_num = maps.size();
905 while (readLine() && line >> c && c != '@') {
908 std::vector<std::string> tokens(map_num);
909 for (int i = 0; i < map_num; ++i) {
910 if (!_reader_bits::readToken(line, tokens[i])) {
911 std::ostringstream msg;
912 msg << "Column not found (" << i + 1 << ")";
913 throw FormatError(msg.str());
916 if (line >> std::ws >> c)
917 throw FormatError("Extra character at the end of line");
921 n = _digraph.addNode();
922 if (label_index != -1)
923 _node_index.insert(std::make_pair(tokens[label_index], n));
925 if (label_index == -1)
926 throw FormatError("Label map not found");
927 typename std::map<std::string, Node>::iterator it =
928 _node_index.find(tokens[label_index]);
929 if (it == _node_index.end()) {
930 std::ostringstream msg;
931 msg << "Node with label not found: " << tokens[label_index];
932 throw FormatError(msg.str());
937 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
938 _node_maps[i].second->set(n, tokens[map_index[i]]);
949 std::vector<int> map_index(_arc_maps.size());
950 int map_num, label_index;
953 if (!readLine() || !(line >> c) || c == '@') {
954 if (readSuccess() && line) line.putback(c);
955 if (!_arc_maps.empty())
956 throw FormatError("Cannot find map names");
962 std::map<std::string, int> maps;
966 while (_reader_bits::readToken(line, map)) {
969 throw FormatError("'-' is not allowed as a map name");
970 else if (line >> std::ws >> c)
971 throw FormatError("Extra character at the end of line");
974 if (maps.find(map) != maps.end()) {
975 std::ostringstream msg;
976 msg << "Multiple occurence of arc map: " << map;
977 throw FormatError(msg.str());
979 maps.insert(std::make_pair(map, index));
983 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
984 std::map<std::string, int>::iterator jt =
985 maps.find(_arc_maps[i].first);
986 if (jt == maps.end()) {
987 std::ostringstream msg;
988 msg << "Map not found: " << _arc_maps[i].first;
989 throw FormatError(msg.str());
991 map_index[i] = jt->second;
995 std::map<std::string, int>::iterator jt = maps.find("label");
996 if (jt != maps.end()) {
997 label_index = jt->second;
1002 map_num = maps.size();
1005 while (readLine() && line >> c && c != '@') {
1008 std::string source_token;
1009 std::string target_token;
1011 if (!_reader_bits::readToken(line, source_token))
1012 throw FormatError("Source not found");
1014 if (!_reader_bits::readToken(line, target_token))
1015 throw FormatError("Target not found");
1017 std::vector<std::string> tokens(map_num);
1018 for (int i = 0; i < map_num; ++i) {
1019 if (!_reader_bits::readToken(line, tokens[i])) {
1020 std::ostringstream msg;
1021 msg << "Column not found (" << i + 1 << ")";
1022 throw FormatError(msg.str());
1025 if (line >> std::ws >> c)
1026 throw FormatError("Extra character at the end of line");
1031 typename NodeIndex::iterator it;
1033 it = _node_index.find(source_token);
1034 if (it == _node_index.end()) {
1035 std::ostringstream msg;
1036 msg << "Item not found: " << source_token;
1037 throw FormatError(msg.str());
1039 Node source = it->second;
1041 it = _node_index.find(target_token);
1042 if (it == _node_index.end()) {
1043 std::ostringstream msg;
1044 msg << "Item not found: " << target_token;
1045 throw FormatError(msg.str());
1047 Node target = it->second;
1049 a = _digraph.addArc(source, target);
1050 if (label_index != -1)
1051 _arc_index.insert(std::make_pair(tokens[label_index], a));
1053 if (label_index == -1)
1054 throw FormatError("Label map not found");
1055 typename std::map<std::string, Arc>::iterator it =
1056 _arc_index.find(tokens[label_index]);
1057 if (it == _arc_index.end()) {
1058 std::ostringstream msg;
1059 msg << "Arc with label not found: " << tokens[label_index];
1060 throw FormatError(msg.str());
1065 for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
1066 _arc_maps[i].second->set(a, tokens[map_index[i]]);
1070 if (readSuccess()) {
1075 void readAttributes() {
1077 std::set<std::string> read_attr;
1080 while (readLine() && line >> c && c != '@') {
1083 std::string attr, token;
1084 if (!_reader_bits::readToken(line, attr))
1085 throw FormatError("Attribute name not found");
1086 if (!_reader_bits::readToken(line, token))
1087 throw FormatError("Attribute value not found");
1089 throw FormatError("Extra character at the end of line");
1092 std::set<std::string>::iterator it = read_attr.find(attr);
1093 if (it != read_attr.end()) {
1094 std::ostringstream msg;
1095 msg << "Multiple occurence of attribute: " << attr;
1096 throw FormatError(msg.str());
1098 read_attr.insert(attr);
1102 typename Attributes::iterator it = _attributes.lower_bound(attr);
1103 while (it != _attributes.end() && it->first == attr) {
1104 it->second->set(token);
1110 if (readSuccess()) {
1113 for (typename Attributes::iterator it = _attributes.begin();
1114 it != _attributes.end(); ++it) {
1115 if (read_attr.find(it->first) == read_attr.end()) {
1116 std::ostringstream msg;
1117 msg << "Attribute not found: " << it->first;
1118 throw FormatError(msg.str());
1125 /// \name Execution of the Reader
1128 /// \brief Start the batch processing
1130 /// This function starts the batch processing
1132 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
1134 bool nodes_done = _skip_nodes;
1135 bool arcs_done = _skip_arcs;
1136 bool attributes_done = false;
1142 while (readSuccess()) {
1145 std::string section, caption;
1147 _reader_bits::readToken(line, section);
1148 _reader_bits::readToken(line, caption);
1151 throw FormatError("Extra character at the end of line");
1153 if (section == "nodes" && !nodes_done) {
1154 if (_nodes_caption.empty() || _nodes_caption == caption) {
1158 } else if ((section == "arcs" || section == "edges") &&
1160 if (_arcs_caption.empty() || _arcs_caption == caption) {
1164 } else if (section == "attributes" && !attributes_done) {
1165 if (_attributes_caption.empty() || _attributes_caption == caption) {
1167 attributes_done = true;
1173 } catch (FormatError& error) {
1174 error.line(line_num);
1175 error.file(_filename);
1181 throw FormatError("Section @nodes not found");
1185 throw FormatError("Section @arcs not found");
1188 if (!attributes_done && !_attributes.empty()) {
1189 throw FormatError("Section @attributes not found");
1198 /// \ingroup lemon_io
1200 /// \brief Return a \ref DigraphReader class
1202 /// This function just returns a \ref DigraphReader class.
1204 /// With this function a digraph can be read from an
1205 /// \ref lgf-format "LGF" file or input stream with several maps and
1206 /// attributes. For example, there is network flow problem on a
1207 /// digraph, i.e. a digraph with a \e capacity map on the arcs and
1208 /// \e source and \e target nodes. This digraph can be read with the
1212 ///ListDigraph digraph;
1213 ///ListDigraph::ArcMap<int> cm(digraph);
1214 ///ListDigraph::Node src, trg;
1215 ///digraphReader(digraph, std::cin).
1216 /// arcMap("capacity", cap).
1217 /// node("source", src).
1218 /// node("target", trg).
1222 /// For a complete documentation, please see the \ref DigraphReader
1223 /// class documentation.
1224 /// \warning Don't forget to put the \ref DigraphReader::run() "run()"
1225 /// to the end of the parameter list.
1226 /// \relates DigraphReader
1227 /// \sa digraphReader(TDGR& digraph, const std::string& fn)
1228 /// \sa digraphReader(TDGR& digraph, const char* fn)
1229 template <typename TDGR>
1230 DigraphReader<TDGR> digraphReader(TDGR& digraph, std::istream& is) {
1231 DigraphReader<TDGR> tmp(digraph, is);
1235 /// \brief Return a \ref DigraphReader class
1237 /// This function just returns a \ref DigraphReader class.
1238 /// \relates DigraphReader
1239 /// \sa digraphReader(TDGR& digraph, std::istream& is)
1240 template <typename TDGR>
1241 DigraphReader<TDGR> digraphReader(TDGR& digraph, const std::string& fn) {
1242 DigraphReader<TDGR> tmp(digraph, fn);
1246 /// \brief Return a \ref DigraphReader class
1248 /// This function just returns a \ref DigraphReader class.
1249 /// \relates DigraphReader
1250 /// \sa digraphReader(TDGR& digraph, std::istream& is)
1251 template <typename TDGR>
1252 DigraphReader<TDGR> digraphReader(TDGR& digraph, const char* fn) {
1253 DigraphReader<TDGR> tmp(digraph, fn);
1257 template <typename GR>
1260 template <typename TGR>
1261 GraphReader<TGR> graphReader(TGR& graph, std::istream& is = std::cin);
1262 template <typename TGR>
1263 GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
1264 template <typename TGR>
1265 GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1267 /// \ingroup lemon_io
1269 /// \brief \ref lgf-format "LGF" reader for undirected graphs
1271 /// This utility reads an \ref lgf-format "LGF" file.
1273 /// It can be used almost the same way as \c DigraphReader.
1274 /// The only difference is that this class can handle edges and
1275 /// edge maps as well as arcs and arc maps.
1277 /// The columns in the \c \@edges (or \c \@arcs) section are the
1278 /// edge maps. However, if there are two maps with the same name
1279 /// prefixed with \c '+' and \c '-', then these can be read into an
1280 /// arc map. Similarly, an attribute can be read into an arc, if
1281 /// it's value is an edge label prefixed with \c '+' or \c '-'.
1282 template <typename GR>
1290 TEMPLATE_GRAPH_TYPEDEFS(GR);
1294 std::string _filename;
1298 std::string _nodes_caption;
1299 std::string _edges_caption;
1300 std::string _attributes_caption;
1302 typedef std::map<std::string, Node> NodeIndex;
1303 NodeIndex _node_index;
1304 typedef std::map<std::string, Edge> EdgeIndex;
1305 EdgeIndex _edge_index;
1307 typedef std::vector<std::pair<std::string,
1308 _reader_bits::MapStorageBase<Node>*> > NodeMaps;
1309 NodeMaps _node_maps;
1311 typedef std::vector<std::pair<std::string,
1312 _reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
1313 EdgeMaps _edge_maps;
1315 typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
1317 Attributes _attributes;
1326 std::istringstream line;
1330 /// \brief Constructor
1332 /// Construct an undirected graph reader, which reads from the given
1334 GraphReader(GR& graph, std::istream& is = std::cin)
1335 : _is(&is), local_is(false), _graph(graph),
1336 _use_nodes(false), _use_edges(false),
1337 _skip_nodes(false), _skip_edges(false) {}
1339 /// \brief Constructor
1341 /// Construct an undirected graph reader, which reads from the given
1343 GraphReader(GR& graph, const std::string& fn)
1344 : _is(new std::ifstream(fn.c_str())), local_is(true),
1345 _filename(fn), _graph(graph),
1346 _use_nodes(false), _use_edges(false),
1347 _skip_nodes(false), _skip_edges(false) {
1350 throw IoError("Cannot open file", fn);
1354 /// \brief Constructor
1356 /// Construct an undirected graph reader, which reads from the given
1358 GraphReader(GR& graph, const char* fn)
1359 : _is(new std::ifstream(fn)), local_is(true),
1360 _filename(fn), _graph(graph),
1361 _use_nodes(false), _use_edges(false),
1362 _skip_nodes(false), _skip_edges(false) {
1365 throw IoError("Cannot open file", fn);
1369 /// \brief Destructor
1371 for (typename NodeMaps::iterator it = _node_maps.begin();
1372 it != _node_maps.end(); ++it) {
1376 for (typename EdgeMaps::iterator it = _edge_maps.begin();
1377 it != _edge_maps.end(); ++it) {
1381 for (typename Attributes::iterator it = _attributes.begin();
1382 it != _attributes.end(); ++it) {
1393 template <typename TGR>
1394 friend GraphReader<TGR> graphReader(TGR& graph, std::istream& is);
1395 template <typename TGR>
1396 friend GraphReader<TGR> graphReader(TGR& graph, const std::string& fn);
1397 template <typename TGR>
1398 friend GraphReader<TGR> graphReader(TGR& graph, const char *fn);
1400 GraphReader(GraphReader& other)
1401 : _is(other._is), local_is(other.local_is), _graph(other._graph),
1402 _use_nodes(other._use_nodes), _use_edges(other._use_edges),
1403 _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) {
1406 other.local_is = false;
1408 _node_index.swap(other._node_index);
1409 _edge_index.swap(other._edge_index);
1411 _node_maps.swap(other._node_maps);
1412 _edge_maps.swap(other._edge_maps);
1413 _attributes.swap(other._attributes);
1415 _nodes_caption = other._nodes_caption;
1416 _edges_caption = other._edges_caption;
1417 _attributes_caption = other._attributes_caption;
1421 GraphReader& operator=(const GraphReader&);
1425 /// \name Reading Rules
1428 /// \brief Node map reading rule
1430 /// Add a node map reading rule to the reader.
1431 template <typename Map>
1432 GraphReader& nodeMap(const std::string& caption, Map& map) {
1433 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1434 _reader_bits::MapStorageBase<Node>* storage =
1435 new _reader_bits::MapStorage<Node, Map>(map);
1436 _node_maps.push_back(std::make_pair(caption, storage));
1440 /// \brief Node map reading rule
1442 /// Add a node map reading rule with specialized converter to the
1444 template <typename Map, typename Converter>
1445 GraphReader& nodeMap(const std::string& caption, Map& map,
1446 const Converter& converter = Converter()) {
1447 checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
1448 _reader_bits::MapStorageBase<Node>* storage =
1449 new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
1450 _node_maps.push_back(std::make_pair(caption, storage));
1454 /// \brief Edge map reading rule
1456 /// Add an edge map reading rule to the reader.
1457 template <typename Map>
1458 GraphReader& edgeMap(const std::string& caption, Map& map) {
1459 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1460 _reader_bits::MapStorageBase<Edge>* storage =
1461 new _reader_bits::MapStorage<Edge, Map>(map);
1462 _edge_maps.push_back(std::make_pair(caption, storage));
1466 /// \brief Edge map reading rule
1468 /// Add an edge map reading rule with specialized converter to the
1470 template <typename Map, typename Converter>
1471 GraphReader& edgeMap(const std::string& caption, Map& map,
1472 const Converter& converter = Converter()) {
1473 checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
1474 _reader_bits::MapStorageBase<Edge>* storage =
1475 new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
1476 _edge_maps.push_back(std::make_pair(caption, storage));
1480 /// \brief Arc map reading rule
1482 /// Add an arc map reading rule to the reader.
1483 template <typename Map>
1484 GraphReader& arcMap(const std::string& caption, Map& map) {
1485 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1486 _reader_bits::MapStorageBase<Edge>* forward_storage =
1487 new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
1488 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1489 _reader_bits::MapStorageBase<Edge>* backward_storage =
1490 new _reader_bits::GraphArcMapStorage<GR, false, Map>(_graph, map);
1491 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1495 /// \brief Arc map reading rule
1497 /// Add an arc map reading rule with specialized converter to the
1499 template <typename Map, typename Converter>
1500 GraphReader& arcMap(const std::string& caption, Map& map,
1501 const Converter& converter = Converter()) {
1502 checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
1503 _reader_bits::MapStorageBase<Edge>* forward_storage =
1504 new _reader_bits::GraphArcMapStorage<GR, true, Map, Converter>
1505 (_graph, map, converter);
1506 _edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
1507 _reader_bits::MapStorageBase<Edge>* backward_storage =
1508 new _reader_bits::GraphArcMapStorage<GR, false, Map, Converter>
1509 (_graph, map, converter);
1510 _edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
1514 /// \brief Attribute reading rule
1516 /// Add an attribute reading rule to the reader.
1517 template <typename Value>
1518 GraphReader& attribute(const std::string& caption, Value& value) {
1519 _reader_bits::ValueStorageBase* storage =
1520 new _reader_bits::ValueStorage<Value>(value);
1521 _attributes.insert(std::make_pair(caption, storage));
1525 /// \brief Attribute reading rule
1527 /// Add an attribute reading rule with specialized converter to the
1529 template <typename Value, typename Converter>
1530 GraphReader& attribute(const std::string& caption, Value& value,
1531 const Converter& converter = Converter()) {
1532 _reader_bits::ValueStorageBase* storage =
1533 new _reader_bits::ValueStorage<Value, Converter>(value, converter);
1534 _attributes.insert(std::make_pair(caption, storage));
1538 /// \brief Node reading rule
1540 /// Add a node reading rule to reader.
1541 GraphReader& node(const std::string& caption, Node& node) {
1542 typedef _reader_bits::MapLookUpConverter<Node> Converter;
1543 Converter converter(_node_index);
1544 _reader_bits::ValueStorageBase* storage =
1545 new _reader_bits::ValueStorage<Node, Converter>(node, converter);
1546 _attributes.insert(std::make_pair(caption, storage));
1550 /// \brief Edge reading rule
1552 /// Add an edge reading rule to reader.
1553 GraphReader& edge(const std::string& caption, Edge& edge) {
1554 typedef _reader_bits::MapLookUpConverter<Edge> Converter;
1555 Converter converter(_edge_index);
1556 _reader_bits::ValueStorageBase* storage =
1557 new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
1558 _attributes.insert(std::make_pair(caption, storage));
1562 /// \brief Arc reading rule
1564 /// Add an arc reading rule to reader.
1565 GraphReader& arc(const std::string& caption, Arc& arc) {
1566 typedef _reader_bits::GraphArcLookUpConverter<GR> Converter;
1567 Converter converter(_graph, _edge_index);
1568 _reader_bits::ValueStorageBase* storage =
1569 new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
1570 _attributes.insert(std::make_pair(caption, storage));
1576 /// \name Select Section by Name
1579 /// \brief Set \c \@nodes section to be read
1581 /// Set \c \@nodes section to be read.
1582 GraphReader& nodes(const std::string& caption) {
1583 _nodes_caption = caption;
1587 /// \brief Set \c \@edges section to be read
1589 /// Set \c \@edges section to be read.
1590 GraphReader& edges(const std::string& caption) {
1591 _edges_caption = caption;
1595 /// \brief Set \c \@attributes section to be read
1597 /// Set \c \@attributes section to be read.
1598 GraphReader& attributes(const std::string& caption) {
1599 _attributes_caption = caption;
1605 /// \name Using Previously Constructed Node or Edge Set
1608 /// \brief Use previously constructed node set
1610 /// Use previously constructed node set, and specify the node
1612 template <typename Map>
1613 GraphReader& useNodes(const Map& map) {
1614 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1615 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1617 _writer_bits::DefaultConverter<typename Map::Value> converter;
1618 for (NodeIt n(_graph); n != INVALID; ++n) {
1619 _node_index.insert(std::make_pair(converter(map[n]), n));
1624 /// \brief Use previously constructed node set
1626 /// Use previously constructed node set, and specify the node
1627 /// label map and a functor which converts the label map values to
1629 template <typename Map, typename Converter>
1630 GraphReader& useNodes(const Map& map,
1631 const Converter& converter = Converter()) {
1632 checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
1633 LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
1635 for (NodeIt n(_graph); n != INVALID; ++n) {
1636 _node_index.insert(std::make_pair(converter(map[n]), n));
1641 /// \brief Use previously constructed edge set
1643 /// Use previously constructed edge set, and specify the edge
1645 template <typename Map>
1646 GraphReader& useEdges(const Map& map) {
1647 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1648 LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1650 _writer_bits::DefaultConverter<typename Map::Value> converter;
1651 for (EdgeIt a(_graph); a != INVALID; ++a) {
1652 _edge_index.insert(std::make_pair(converter(map[a]), a));
1657 /// \brief Use previously constructed edge set
1659 /// Use previously constructed edge set, and specify the edge
1660 /// label map and a functor which converts the label map values to
1662 template <typename Map, typename Converter>
1663 GraphReader& useEdges(const Map& map,
1664 const Converter& converter = Converter()) {
1665 checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
1666 LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
1668 for (EdgeIt a(_graph); a != INVALID; ++a) {
1669 _edge_index.insert(std::make_pair(converter(map[a]), a));
1674 /// \brief Skip the reading of node section
1676 /// Omit the reading of the node section. This implies that each node
1677 /// map reading rule will be abandoned, and the nodes of the graph
1678 /// will not be constructed, which usually cause that the edge set
1679 /// could not be read due to lack of node name
1680 /// could not be read due to lack of node name resolving.
1681 /// Therefore \c skipEdges() function should also be used, or
1682 /// \c useNodes() should be used to specify the label of the nodes.
1683 GraphReader& skipNodes() {
1684 LEMON_ASSERT(!_skip_nodes, "Skip nodes already set");
1689 /// \brief Skip the reading of edge section
1691 /// Omit the reading of the edge section. This implies that each edge
1692 /// map reading rule will be abandoned, and the edges of the graph
1693 /// will not be constructed.
1694 GraphReader& skipEdges() {
1695 LEMON_ASSERT(!_skip_edges, "Skip edges already set");
1706 while(++line_num, std::getline(*_is, str)) {
1707 line.clear(); line.str(str);
1709 if (line >> std::ws >> c && c != '#') {
1717 bool readSuccess() {
1718 return static_cast<bool>(*_is);
1721 void skipSection() {
1723 while (readSuccess() && line >> c && c != '@') {
1726 if (readSuccess()) {
1733 std::vector<int> map_index(_node_maps.size());
1734 int map_num, label_index;
1737 if (!readLine() || !(line >> c) || c == '@') {
1738 if (readSuccess() && line) line.putback(c);
1739 if (!_node_maps.empty())
1740 throw FormatError("Cannot find map names");
1746 std::map<std::string, int> maps;
1750 while (_reader_bits::readToken(line, map)) {
1751 if (maps.find(map) != maps.end()) {
1752 std::ostringstream msg;
1753 msg << "Multiple occurence of node map: " << map;
1754 throw FormatError(msg.str());
1756 maps.insert(std::make_pair(map, index));
1760 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
1761 std::map<std::string, int>::iterator jt =
1762 maps.find(_node_maps[i].first);
1763 if (jt == maps.end()) {
1764 std::ostringstream msg;
1765 msg << "Map not found: " << _node_maps[i].first;
1766 throw FormatError(msg.str());
1768 map_index[i] = jt->second;
1772 std::map<std::string, int>::iterator jt = maps.find("label");
1773 if (jt != maps.end()) {
1774 label_index = jt->second;
1779 map_num = maps.size();
1782 while (readLine() && line >> c && c != '@') {
1785 std::vector<std::string> tokens(map_num);
1786 for (int i = 0; i < map_num; ++i) {
1787 if (!_reader_bits::readToken(line, tokens[i])) {
1788 std::ostringstream msg;
1789 msg << "Column not found (" << i + 1 << ")";
1790 throw FormatError(msg.str());
1793 if (line >> std::ws >> c)
1794 throw FormatError("Extra character at the end of line");
1798 n = _graph.addNode();
1799 if (label_index != -1)
1800 _node_index.insert(std::make_pair(tokens[label_index], n));
1802 if (label_index == -1)
1803 throw FormatError("Label map not found");
1804 typename std::map<std::string, Node>::iterator it =
1805 _node_index.find(tokens[label_index]);
1806 if (it == _node_index.end()) {
1807 std::ostringstream msg;
1808 msg << "Node with label not found: " << tokens[label_index];
1809 throw FormatError(msg.str());
1814 for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
1815 _node_maps[i].second->set(n, tokens[map_index[i]]);
1819 if (readSuccess()) {
1826 std::vector<int> map_index(_edge_maps.size());
1827 int map_num, label_index;
1830 if (!readLine() || !(line >> c) || c == '@') {
1831 if (readSuccess() && line) line.putback(c);
1832 if (!_edge_maps.empty())
1833 throw FormatError("Cannot find map names");
1839 std::map<std::string, int> maps;
1843 while (_reader_bits::readToken(line, map)) {
1846 throw FormatError("'-' is not allowed as a map name");
1847 else if (line >> std::ws >> c)
1848 throw FormatError("Extra character at the end of line");
1851 if (maps.find(map) != maps.end()) {
1852 std::ostringstream msg;
1853 msg << "Multiple occurence of edge map: " << map;
1854 throw FormatError(msg.str());
1856 maps.insert(std::make_pair(map, index));
1860 for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1861 std::map<std::string, int>::iterator jt =
1862 maps.find(_edge_maps[i].first);
1863 if (jt == maps.end()) {
1864 std::ostringstream msg;
1865 msg << "Map not found: " << _edge_maps[i].first;
1866 throw FormatError(msg.str());
1868 map_index[i] = jt->second;
1872 std::map<std::string, int>::iterator jt = maps.find("label");
1873 if (jt != maps.end()) {
1874 label_index = jt->second;
1879 map_num = maps.size();
1882 while (readLine() && line >> c && c != '@') {
1885 std::string source_token;
1886 std::string target_token;
1888 if (!_reader_bits::readToken(line, source_token))
1889 throw FormatError("Node u not found");
1891 if (!_reader_bits::readToken(line, target_token))
1892 throw FormatError("Node v not found");
1894 std::vector<std::string> tokens(map_num);
1895 for (int i = 0; i < map_num; ++i) {
1896 if (!_reader_bits::readToken(line, tokens[i])) {
1897 std::ostringstream msg;
1898 msg << "Column not found (" << i + 1 << ")";
1899 throw FormatError(msg.str());
1902 if (line >> std::ws >> c)
1903 throw FormatError("Extra character at the end of line");
1908 typename NodeIndex::iterator it;
1910 it = _node_index.find(source_token);
1911 if (it == _node_index.end()) {
1912 std::ostringstream msg;
1913 msg << "Item not found: " << source_token;
1914 throw FormatError(msg.str());
1916 Node source = it->second;
1918 it = _node_index.find(target_token);
1919 if (it == _node_index.end()) {
1920 std::ostringstream msg;
1921 msg << "Item not found: " << target_token;
1922 throw FormatError(msg.str());
1924 Node target = it->second;
1926 e = _graph.addEdge(source, target);
1927 if (label_index != -1)
1928 _edge_index.insert(std::make_pair(tokens[label_index], e));
1930 if (label_index == -1)
1931 throw FormatError("Label map not found");
1932 typename std::map<std::string, Edge>::iterator it =
1933 _edge_index.find(tokens[label_index]);
1934 if (it == _edge_index.end()) {
1935 std::ostringstream msg;
1936 msg << "Edge with label not found: " << tokens[label_index];
1937 throw FormatError(msg.str());
1942 for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
1943 _edge_maps[i].second->set(e, tokens[map_index[i]]);
1947 if (readSuccess()) {
1952 void readAttributes() {
1954 std::set<std::string> read_attr;
1957 while (readLine() && line >> c && c != '@') {
1960 std::string attr, token;
1961 if (!_reader_bits::readToken(line, attr))
1962 throw FormatError("Attribute name not found");
1963 if (!_reader_bits::readToken(line, token))
1964 throw FormatError("Attribute value not found");
1966 throw FormatError("Extra character at the end of line");
1969 std::set<std::string>::iterator it = read_attr.find(attr);
1970 if (it != read_attr.end()) {
1971 std::ostringstream msg;
1972 msg << "Multiple occurence of attribute: " << attr;
1973 throw FormatError(msg.str());
1975 read_attr.insert(attr);
1979 typename Attributes::iterator it = _attributes.lower_bound(attr);
1980 while (it != _attributes.end() && it->first == attr) {
1981 it->second->set(token);
1987 if (readSuccess()) {
1990 for (typename Attributes::iterator it = _attributes.begin();
1991 it != _attributes.end(); ++it) {
1992 if (read_attr.find(it->first) == read_attr.end()) {
1993 std::ostringstream msg;
1994 msg << "Attribute not found: " << it->first;
1995 throw FormatError(msg.str());
2002 /// \name Execution of the Reader
2005 /// \brief Start the batch processing
2007 /// This function starts the batch processing
2010 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
2012 bool nodes_done = _skip_nodes;
2013 bool edges_done = _skip_edges;
2014 bool attributes_done = false;
2020 while (readSuccess()) {
2023 std::string section, caption;
2025 _reader_bits::readToken(line, section);
2026 _reader_bits::readToken(line, caption);
2029 throw FormatError("Extra character at the end of line");
2031 if (section == "nodes" && !nodes_done) {
2032 if (_nodes_caption.empty() || _nodes_caption == caption) {
2036 } else if ((section == "edges" || section == "arcs") &&
2038 if (_edges_caption.empty() || _edges_caption == caption) {
2042 } else if (section == "attributes" && !attributes_done) {
2043 if (_attributes_caption.empty() || _attributes_caption == caption) {
2045 attributes_done = true;
2051 } catch (FormatError& error) {
2052 error.line(line_num);
2053 error.file(_filename);
2059 throw FormatError("Section @nodes not found");
2063 throw FormatError("Section @edges not found");
2066 if (!attributes_done && !_attributes.empty()) {
2067 throw FormatError("Section @attributes not found");
2076 /// \ingroup lemon_io
2078 /// \brief Return a \ref GraphReader class
2080 /// This function just returns a \ref GraphReader class.
2082 /// With this function a graph can be read from an
2083 /// \ref lgf-format "LGF" file or input stream with several maps and
2084 /// attributes. For example, there is weighted matching problem on a
2085 /// graph, i.e. a graph with a \e weight map on the edges. This
2086 /// graph can be read with the following code:
2090 ///ListGraph::EdgeMap<int> weight(graph);
2091 ///graphReader(graph, std::cin).
2092 /// edgeMap("weight", weight).
2096 /// For a complete documentation, please see the \ref GraphReader
2097 /// class documentation.
2098 /// \warning Don't forget to put the \ref GraphReader::run() "run()"
2099 /// to the end of the parameter list.
2100 /// \relates GraphReader
2101 /// \sa graphReader(TGR& graph, const std::string& fn)
2102 /// \sa graphReader(TGR& graph, const char* fn)
2103 template <typename TGR>
2104 GraphReader<TGR> graphReader(TGR& graph, std::istream& is) {
2105 GraphReader<TGR> tmp(graph, is);
2109 /// \brief Return a \ref GraphReader class
2111 /// This function just returns a \ref GraphReader class.
2112 /// \relates GraphReader
2113 /// \sa graphReader(TGR& graph, std::istream& is)
2114 template <typename TGR>
2115 GraphReader<TGR> graphReader(TGR& graph, const std::string& fn) {
2116 GraphReader<TGR> tmp(graph, fn);
2120 /// \brief Return a \ref GraphReader class
2122 /// This function just returns a \ref GraphReader class.
2123 /// \relates GraphReader
2124 /// \sa graphReader(TGR& graph, std::istream& is)
2125 template <typename TGR>
2126 GraphReader<TGR> graphReader(TGR& graph, const char* fn) {
2127 GraphReader<TGR> tmp(graph, fn);
2131 class SectionReader;
2133 SectionReader sectionReader(std::istream& is);
2134 SectionReader sectionReader(const std::string& fn);
2135 SectionReader sectionReader(const char* fn);
2137 /// \ingroup lemon_io
2139 /// \brief Section reader class
2141 /// In the \ref lgf-format "LGF" file extra sections can be placed,
2142 /// which contain any data in arbitrary format. Such sections can be
2143 /// read with this class. A reading rule can be added to the class
2144 /// with two different functions. With the \c sectionLines() function a
2145 /// functor can process the section line-by-line, while with the \c
2146 /// sectionStream() member the section can be read from an input
2148 class SectionReader {
2153 std::string _filename;
2155 typedef std::map<std::string, _reader_bits::Section*> Sections;
2159 std::istringstream line;
2163 /// \brief Constructor
2165 /// Construct a section reader, which reads from the given input
2167 SectionReader(std::istream& is)
2168 : _is(&is), local_is(false) {}
2170 /// \brief Constructor
2172 /// Construct a section reader, which reads from the given file.
2173 SectionReader(const std::string& fn)
2174 : _is(new std::ifstream(fn.c_str())), local_is(true),
2178 throw IoError("Cannot open file", fn);
2182 /// \brief Constructor
2184 /// Construct a section reader, which reads from the given file.
2185 SectionReader(const char* fn)
2186 : _is(new std::ifstream(fn)), local_is(true),
2190 throw IoError("Cannot open file", fn);
2194 /// \brief Destructor
2196 for (Sections::iterator it = _sections.begin();
2197 it != _sections.end(); ++it) {
2209 friend SectionReader sectionReader(std::istream& is);
2210 friend SectionReader sectionReader(const std::string& fn);
2211 friend SectionReader sectionReader(const char* fn);
2213 SectionReader(SectionReader& other)
2214 : _is(other._is), local_is(other.local_is) {
2217 other.local_is = false;
2219 _sections.swap(other._sections);
2222 SectionReader& operator=(const SectionReader&);
2226 /// \name Section Readers
2229 /// \brief Add a section processor with line oriented reading
2231 /// The first parameter is the type descriptor of the section, the
2232 /// second is a functor, which takes just one \c std::string
2233 /// parameter. At the reading process, each line of the section
2234 /// will be given to the functor object. However, the empty lines
2235 /// and the comment lines are filtered out, and the leading
2236 /// whitespaces are trimmed from each processed string.
2238 /// For example, let's see a section, which contain several
2239 /// integers, which should be inserted into a vector.
2247 /// The functor is implemented as a struct:
2249 /// struct NumberSection {
2250 /// std::vector<int>& _data;
2251 /// NumberSection(std::vector<int>& data) : _data(data) {}
2252 /// void operator()(const std::string& line) {
2253 /// std::istringstream ls(line);
2255 /// while (ls >> value) _data.push_back(value);
2261 /// reader.sectionLines("numbers", NumberSection(vec));
2263 template <typename Functor>
2264 SectionReader& sectionLines(const std::string& type, Functor functor) {
2265 LEMON_ASSERT(!type.empty(), "Type is empty.");
2266 LEMON_ASSERT(_sections.find(type) == _sections.end(),
2267 "Multiple reading of section.");
2268 _sections.insert(std::make_pair(type,
2269 new _reader_bits::LineSection<Functor>(functor)));
2274 /// \brief Add a section processor with stream oriented reading
2276 /// The first parameter is the type of the section, the second is
2277 /// a functor, which takes an \c std::istream& and an \c int&
2278 /// parameter, the latter regard to the line number of stream. The
2279 /// functor can read the input while the section go on, and the
2280 /// line number should be modified accordingly.
2281 template <typename Functor>
2282 SectionReader& sectionStream(const std::string& type, Functor functor) {
2283 LEMON_ASSERT(!type.empty(), "Type is empty.");
2284 LEMON_ASSERT(_sections.find(type) == _sections.end(),
2285 "Multiple reading of section.");
2286 _sections.insert(std::make_pair(type,
2287 new _reader_bits::StreamSection<Functor>(functor)));
2297 while(++line_num, std::getline(*_is, str)) {
2298 line.clear(); line.str(str);
2300 if (line >> std::ws >> c && c != '#') {
2308 bool readSuccess() {
2309 return static_cast<bool>(*_is);
2312 void skipSection() {
2314 while (readSuccess() && line >> c && c != '@') {
2317 if (readSuccess()) {
2325 /// \name Execution of the Reader
2328 /// \brief Start the batch processing
2330 /// This function starts the batch processing.
2333 LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
2335 std::set<std::string> extra_sections;
2341 while (readSuccess()) {
2344 std::string section, caption;
2346 _reader_bits::readToken(line, section);
2347 _reader_bits::readToken(line, caption);
2350 throw FormatError("Extra character at the end of line");
2352 if (extra_sections.find(section) != extra_sections.end()) {
2353 std::ostringstream msg;
2354 msg << "Multiple occurence of section: " << section;
2355 throw FormatError(msg.str());
2357 Sections::iterator it = _sections.find(section);
2358 if (it != _sections.end()) {
2359 extra_sections.insert(section);
2360 it->second->process(*_is, line_num);
2364 } catch (FormatError& error) {
2365 error.line(line_num);
2366 error.file(_filename);
2370 for (Sections::iterator it = _sections.begin();
2371 it != _sections.end(); ++it) {
2372 if (extra_sections.find(it->first) == extra_sections.end()) {
2373 std::ostringstream os;
2374 os << "Cannot find section: " << it->first;
2375 throw FormatError(os.str());
2384 /// \ingroup lemon_io
2386 /// \brief Return a \ref SectionReader class
2388 /// This function just returns a \ref SectionReader class.
2390 /// Please see SectionReader documentation about the custom section
2393 /// \relates SectionReader
2394 /// \sa sectionReader(const std::string& fn)
2395 /// \sa sectionReader(const char *fn)
2396 inline SectionReader sectionReader(std::istream& is) {
2397 SectionReader tmp(is);
2401 /// \brief Return a \ref SectionReader class
2403 /// This function just returns a \ref SectionReader class.
2404 /// \relates SectionReader
2405 /// \sa sectionReader(std::istream& is)
2406 inline SectionReader sectionReader(const std::string& fn) {
2407 SectionReader tmp(fn);
2411 /// \brief Return a \ref SectionReader class
2413 /// This function just returns a \ref SectionReader class.
2414 /// \relates SectionReader
2415 /// \sa sectionReader(std::istream& is)
2416 inline SectionReader sectionReader(const char* fn) {
2417 SectionReader tmp(fn);
2421 /// \ingroup lemon_io
2423 /// \brief Reader for the contents of the \ref lgf-format "LGF" file
2425 /// This class can be used to read the sections, the map names and
2426 /// the attributes from a file. Usually, the LEMON programs know
2427 /// that, which type of graph, which maps and which attributes
2428 /// should be read from a file, but in general tools (like glemon)
2429 /// the contents of an LGF file should be guessed somehow. This class
2430 /// reads the graph and stores the appropriate information for
2431 /// reading the graph.
2434 /// LgfContents contents("graph.lgf");
2437 /// // Does it contain any node section and arc section?
2438 /// if (contents.nodeSectionNum() == 0 || contents.arcSectionNum()) {
2439 /// std::cerr << "Failure, cannot find graph." << std::endl;
2442 /// std::cout << "The name of the default node section: "
2443 /// << contents.nodeSection(0) << std::endl;
2444 /// std::cout << "The number of the arc maps: "
2445 /// << contents.arcMaps(0).size() << std::endl;
2446 /// std::cout << "The name of second arc map: "
2447 /// << contents.arcMaps(0)[1] << std::endl;
2455 std::vector<std::string> _node_sections;
2456 std::vector<std::string> _edge_sections;
2457 std::vector<std::string> _attribute_sections;
2458 std::vector<std::string> _extra_sections;
2460 std::vector<bool> _arc_sections;
2462 std::vector<std::vector<std::string> > _node_maps;
2463 std::vector<std::vector<std::string> > _edge_maps;
2465 std::vector<std::vector<std::string> > _attributes;
2469 std::istringstream line;
2473 /// \brief Constructor
2475 /// Construct an \e LGF contents reader, which reads from the given
2477 LgfContents(std::istream& is)
2478 : _is(&is), local_is(false) {}
2480 /// \brief Constructor
2482 /// Construct an \e LGF contents reader, which reads from the given
2484 LgfContents(const std::string& fn)
2485 : _is(new std::ifstream(fn.c_str())), local_is(true) {
2488 throw IoError("Cannot open file", fn);
2492 /// \brief Constructor
2494 /// Construct an \e LGF contents reader, which reads from the given
2496 LgfContents(const char* fn)
2497 : _is(new std::ifstream(fn)), local_is(true) {
2500 throw IoError("Cannot open file", fn);
2504 /// \brief Destructor
2506 if (local_is) delete _is;
2511 LgfContents(const LgfContents&);
2512 LgfContents& operator=(const LgfContents&);
2517 /// \name Node Sections
2520 /// \brief Gives back the number of node sections in the file.
2522 /// Gives back the number of node sections in the file.
2523 int nodeSectionNum() const {
2524 return _node_sections.size();
2527 /// \brief Returns the node section name at the given position.
2529 /// Returns the node section name at the given position.
2530 const std::string& nodeSection(int i) const {
2531 return _node_sections[i];
2534 /// \brief Gives back the node maps for the given section.
2536 /// Gives back the node maps for the given section.
2537 const std::vector<std::string>& nodeMapNames(int i) const {
2538 return _node_maps[i];
2543 /// \name Arc/Edge Sections
2546 /// \brief Gives back the number of arc/edge sections in the file.
2548 /// Gives back the number of arc/edge sections in the file.
2549 /// \note It is synonym of \c edgeSectionNum().
2550 int arcSectionNum() const {
2551 return _edge_sections.size();
2554 /// \brief Returns the arc/edge section name at the given position.
2556 /// Returns the arc/edge section name at the given position.
2557 /// \note It is synonym of \c edgeSection().
2558 const std::string& arcSection(int i) const {
2559 return _edge_sections[i];
2562 /// \brief Gives back the arc/edge maps for the given section.
2564 /// Gives back the arc/edge maps for the given section.
2565 /// \note It is synonym of \c edgeMapNames().
2566 const std::vector<std::string>& arcMapNames(int i) const {
2567 return _edge_maps[i];
2575 /// \brief Gives back the number of arc/edge sections in the file.
2577 /// Gives back the number of arc/edge sections in the file.
2578 /// \note It is synonym of \c arcSectionNum().
2579 int edgeSectionNum() const {
2580 return _edge_sections.size();
2583 /// \brief Returns the section name at the given position.
2585 /// Returns the section name at the given position.
2586 /// \note It is synonym of \c arcSection().
2587 const std::string& edgeSection(int i) const {
2588 return _edge_sections[i];
2591 /// \brief Gives back the edge maps for the given section.
2593 /// Gives back the edge maps for the given section.
2594 /// \note It is synonym of \c arcMapNames().
2595 const std::vector<std::string>& edgeMapNames(int i) const {
2596 return _edge_maps[i];
2601 /// \name Attribute Sections
2604 /// \brief Gives back the number of attribute sections in the file.
2606 /// Gives back the number of attribute sections in the file.
2607 int attributeSectionNum() const {
2608 return _attribute_sections.size();
2611 /// \brief Returns the attribute section name at the given position.
2613 /// Returns the attribute section name at the given position.
2614 const std::string& attributeSectionNames(int i) const {
2615 return _attribute_sections[i];
2618 /// \brief Gives back the attributes for the given section.
2620 /// Gives back the attributes for the given section.
2621 const std::vector<std::string>& attributes(int i) const {
2622 return _attributes[i];
2627 /// \name Extra Sections
2630 /// \brief Gives back the number of extra sections in the file.
2632 /// Gives back the number of extra sections in the file.
2633 int extraSectionNum() const {
2634 return _extra_sections.size();
2637 /// \brief Returns the extra section type at the given position.
2639 /// Returns the section type at the given position.
2640 const std::string& extraSection(int i) const {
2641 return _extra_sections[i];
2650 while(++line_num, std::getline(*_is, str)) {
2651 line.clear(); line.str(str);
2653 if (line >> std::ws >> c && c != '#') {
2661 bool readSuccess() {
2662 return static_cast<bool>(*_is);
2665 void skipSection() {
2667 while (readSuccess() && line >> c && c != '@') {
2670 if (readSuccess()) {
2675 void readMaps(std::vector<std::string>& maps) {
2677 if (!readLine() || !(line >> c) || c == '@') {
2678 if (readSuccess() && line) line.putback(c);
2683 while (_reader_bits::readToken(line, map)) {
2684 maps.push_back(map);
2688 void readAttributes(std::vector<std::string>& attrs) {
2691 while (readSuccess() && line >> c && c != '@') {
2694 _reader_bits::readToken(line, attr);
2695 attrs.push_back(attr);
2703 /// \name Execution of the Contents Reader
2706 /// \brief Starts the reading
2708 /// This function starts the reading.
2714 while (readSuccess()) {
2719 std::string section, caption;
2720 _reader_bits::readToken(line, section);
2721 _reader_bits::readToken(line, caption);
2723 if (section == "nodes") {
2724 _node_sections.push_back(caption);
2725 _node_maps.push_back(std::vector<std::string>());
2726 readMaps(_node_maps.back());
2727 readLine(); skipSection();
2728 } else if (section == "arcs" || section == "edges") {
2729 _edge_sections.push_back(caption);
2730 _arc_sections.push_back(section == "arcs");
2731 _edge_maps.push_back(std::vector<std::string>());
2732 readMaps(_edge_maps.back());
2733 readLine(); skipSection();
2734 } else if (section == "attributes") {
2735 _attribute_sections.push_back(caption);
2736 _attributes.push_back(std::vector<std::string>());
2737 readAttributes(_attributes.back());
2739 _extra_sections.push_back(section);
2740 readLine(); skipSection();