* This file is a part of LEMON, a generic C++ optimization library
* Copyright (C) 2003-2008
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
///\brief Lemon Graph Format reader.
#ifndef LEMON_LGF_READER_H
#define LEMON_LGF_READER_H
#include <lemon/assert.h>
#include <lemon/graph_utils.h>
#include <lemon/lgf_writer.h>
#include <lemon/concept_check.h>
#include <lemon/concepts/maps.h>
template <typename Value>
struct DefaultConverter {
Value operator()(const std::string& str) {
std::istringstream is(str);
if (is >> std::ws >> c) {
throw DataFormatError("Remaining characters in token");
struct DefaultConverter<std::string> {
std::string operator()(const std::string& str) {
template <typename _Item>
virtual ~MapStorageBase() {}
virtual void set(const Item& item, const std::string& value) = 0;
template <typename _Item, typename _Map,
typename _Converter = DefaultConverter<typename _Map::Value> >
class MapStorage : public MapStorageBase<_Item> {
typedef _Converter Converter;
MapStorage(Map& map, const Converter& converter = Converter())
: _map(map), _converter(converter) {}
virtual void set(const Item& item ,const std::string& value) {
_map.set(item, _converter(value));
template <typename _Graph, bool _dir, typename _Map,
typename _Converter = DefaultConverter<typename _Map::Value> >
class GraphArcMapStorage : public MapStorageBase<typename _Graph::Edge> {
typedef _Converter Converter;
typedef typename Graph::Edge Item;
static const bool dir = _dir;
GraphArcMapStorage(const Graph& graph, Map& map,
const Converter& converter = Converter())
: _graph(graph), _map(map), _converter(converter) {}
virtual ~GraphArcMapStorage() {}
virtual void set(const Item& item ,const std::string& value) {
_map.set(_graph.direct(item, dir), _converter(value));
virtual ~ValueStorageBase() {}
virtual void set(const std::string&) = 0;
template <typename _Value, typename _Converter = DefaultConverter<_Value> >
class ValueStorage : public ValueStorageBase {
typedef _Converter Converter;
ValueStorage(Value& value, const Converter& converter = Converter())
: _value(value), _converter(converter) {}
virtual void set(const std::string& value) {
_value = _converter(value);
template <typename Value>
struct MapLookUpConverter {
const std::map<std::string, Value>& _map;
MapLookUpConverter(const std::map<std::string, Value>& map)
Value operator()(const std::string& str) {
typename std::map<std::string, Value>::const_iterator it =
msg << "Item not found: " << str;
throw DataFormatError(msg.str().c_str());
template <typename Graph>
struct GraphArcLookUpConverter {
const std::map<std::string, typename Graph::Edge>& _map;
GraphArcLookUpConverter(const Graph& graph,
const std::map<std::string,
typename Graph::Edge>& map)
: _graph(graph), _map(map) {}
typename Graph::Arc operator()(const std::string& str) {
if (str.empty() || (str[0] != '+' && str[0] != '-')) {
throw DataFormatError("Item must start with '+' or '-'");
typename std::map<std::string, typename Graph::Edge>
::const_iterator it = _map.find(str.substr(1));
throw DataFormatError("Item not found");
return _graph.direct(it->second, str[0] == '+');
bool isWhiteSpace(char c) {
return c == ' ' || c == '\t' || c == '\v' ||
c == '\n' || c == '\r' || c == '\f';
return '0' <= c && c <='7';
LEMON_ASSERT(isOct(c), "The character is not octal.");
return ('0' <= c && c <= '9') ||
('a' <= c && c <= 'z') ||
LEMON_ASSERT(isHex(c), "The character is not hexadecimal.");
if ('0' <= c && c <= '9') return c - '0';
if ('a' <= c && c <= 'z') return c - 'a' + 10;
bool isIdentifierFirstChar(char c) {
return ('a' <= c && c <= 'z') ||
('A' <= c && c <= 'Z') || c == '_';
bool isIdentifierChar(char c) {
return isIdentifierFirstChar(c) ||
char readEscape(std::istream& is) {
throw DataFormatError("Escape format error");
if (!is.get(c) || !isHex(c))
throw DataFormatError("Escape format error");
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
else code = code * 16 + valueHex(c);
throw DataFormatError("Escape format error");
else if (code = valueOct(c), !is.get(c) || !isOct(c))
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
else code = code * 8 + valueOct(c);
std::istream& readToken(std::istream& is, std::string& str) {
while (is.get(c) && c != '\"') {
throw DataFormatError("Quoted format error");
while (is.get(c) && !isWhiteSpace(c)) {
virtual void process(std::istream& is, int& line_num) = 0;
template <typename Functor>
class LineSection : public Section {
LineSection(const Functor& functor) : _functor(functor) {}
virtual ~LineSection() {}
virtual void process(std::istream& is, int& line_num) {
while (is.get(c) && c != '@') {
} else if (!isWhiteSpace(c)) {
else if (is.eof()) is.clear();
template <typename Functor>
class StreamSection : public Section {
StreamSection(const Functor& functor) : _functor(functor) {}
virtual ~StreamSection() {}
virtual void process(std::istream& is, int& line_num) {
while (is.get(c) && c != '@') {
} else if (!isWhiteSpace(c)) {
else if (is.eof()) is.clear();
/// \brief LGF reader for directed graphs
/// This utility reads an \ref lgf-format "LGF" file.
/// The reading method does a batch processing. The user creates a
/// reader object, then various reading rules can be added to the
/// reader, and eventually the reading is executed with the \c run()
/// member function. A map reading rule can be added to the reader
/// with the \c nodeMap() or \c arcMap() members. An optional
/// converter parameter can also be added as a standard functor
/// converting from std::string to the value type of the map. If it
/// is set, it will determine how the tokens in the file should be
/// is converted to the map's value type. If the functor is not set,
/// then a default conversion will be used. One map can be read into
/// multiple map objects at the same time. The \c attribute(), \c
/// node() and \c arc() functions are used to add attribute reading
/// DigraphReader<Digraph>(std::cin, digraph).
/// nodeMap("coordinates", coord_map).
/// arcMap("capacity", cap_map).
/// attribute("caption", caption).
/// By default the reader uses the first section in the file of the
/// proper type. If a section has an optional name, then it can be
/// selected for reading by giving an optional name parameter to the
/// \c nodes(), \c arcs() or \c attributes() functions. The readers
/// also can load extra sections with the \c sectionLines() and
/// sectionStream() functions.
/// The \c useNodes() and \c useArcs() functions are used to tell the reader
/// that the nodes or arcs should not be constructed (added to the
/// graph) during the reading, but instead the label map of the items
/// are given as a parameter of these functions. An
/// application of these function is multipass reading, which is
/// important if two \e \@arcs sections must be read from the
/// file. In this example the first phase would read the node set and one
/// of the arc sets, while the second phase would read the second arc
/// set into an \e ArcSet class (\c SmartArcSet or \c ListArcSet).
/// The previously read label node map should be passed to the \c
/// useNodes() functions. Another application of multipass reading when
/// paths are given as a node map or an arc map. It is impossible read this in
/// a single pass, because the arcs are not constructed when the node
template <typename _Digraph>
typedef _Digraph Digraph;
TEMPLATE_DIGRAPH_TYPEDEFS(Digraph);
std::string _nodes_caption;
std::string _arcs_caption;
std::string _attributes_caption;
typedef std::map<std::string, Node> NodeIndex;
typedef std::map<std::string, Arc> ArcIndex;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Arc>*> >ArcMaps;
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
typedef std::map<std::string, _reader_bits::Section*> Sections;
/// Construct a directed graph reader, which reads from the given
DigraphReader(std::istream& is, Digraph& digraph)
: _is(&is), local_is(false), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// Construct a directed graph reader, which reads from the given
DigraphReader(const std::string& fn, Digraph& digraph)
: _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// Construct a directed graph reader, which reads from the given
DigraphReader(const char* fn, Digraph& digraph)
: _is(new std::ifstream(fn)), local_is(true), _digraph(digraph),
_use_nodes(false), _use_arcs(false) {}
/// \brief Copy constructor
/// The copy constructor transfers all data from the other reader,
/// therefore the copied reader will not be usable more.
DigraphReader(DigraphReader& other)
: _is(other._is), local_is(other.local_is), _digraph(other._digraph),
_use_nodes(other._use_nodes), _use_arcs(other._use_arcs) {
_node_index.swap(other._node_index);
_arc_index.swap(other._arc_index);
_node_maps.swap(other._node_maps);
_arc_maps.swap(other._arc_maps);
_attributes.swap(other._attributes);
_nodes_caption = other._nodes_caption;
_arcs_caption = other._arcs_caption;
_attributes_caption = other._attributes_caption;
_sections.swap(other._sections);
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
for (typename ArcMaps::iterator it = _arc_maps.begin();
it != _arc_maps.end(); ++it) {
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
for (typename Sections::iterator it = _sections.begin();
it != _sections.end(); ++it) {
DigraphReader& operator=(const DigraphReader&);
/// \brief Node map reading rule
/// Add a node map reading rule to the reader.
DigraphReader& nodeMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map>(map);
_node_maps.push_back(std::make_pair(caption, storage));
/// \brief Node map reading rule
/// Add a node map reading rule with specialized converter to the
template <typename Map, typename Converter>
DigraphReader& nodeMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
_node_maps.push_back(std::make_pair(caption, storage));
/// \brief Arc map reading rule
/// Add an arc map reading rule to the reader.
DigraphReader& arcMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Arc>* storage =
new _reader_bits::MapStorage<Arc, Map>(map);
_arc_maps.push_back(std::make_pair(caption, storage));
/// \brief Arc map reading rule
/// Add an arc map reading rule with specialized converter to the
template <typename Map, typename Converter>
DigraphReader& arcMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Arc>* storage =
new _reader_bits::MapStorage<Arc, Map, Converter>(map, converter);
_arc_maps.push_back(std::make_pair(caption, storage));
/// \brief Attribute reading rule
/// Add an attribute reading rule to the reader.
template <typename Value>
DigraphReader& attribute(const std::string& caption, Value& value) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value>(value);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Attribute reading rule
/// Add an attribute reading rule with specialized converter to the
template <typename Value, typename Converter>
DigraphReader& attribute(const std::string& caption, Value& value,
const Converter& converter = Converter()) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value, Converter>(value, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Node reading rule
/// Add a node reading rule to reader.
DigraphReader& node(const std::string& caption, Node& node) {
typedef _reader_bits::MapLookUpConverter<Node> Converter;
Converter converter(_node_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Node, Converter>(node, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Arc reading rule
/// Add an arc reading rule to reader.
DigraphReader& arc(const std::string& caption, Arc& arc) {
typedef _reader_bits::MapLookUpConverter<Arc> Converter;
Converter converter(_arc_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \name Select section by name
/// \brief Set \c \@nodes section to be read
/// Set \c \@nodes section to be read
DigraphReader& nodes(const std::string& caption) {
_nodes_caption = caption;
/// \brief Set \c \@arcs section to be read
/// Set \c \@arcs section to be read
DigraphReader& arcs(const std::string& caption) {
/// \brief Set \c \@attributes section to be read
/// Set \c \@attributes section to be read
DigraphReader& attributes(const std::string& caption) {
_attributes_caption = caption;
/// \name Section readers
/// \brief Add a section processor with line oriented reading
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read with
/// this function line by line. The first parameter is the type
/// descriptor of the section, the second is a functor, which
/// takes just one \c std::string parameter. At the reading
/// process, each line of the section will be given to the functor
/// object. However, the empty lines and the comment lines are
/// filtered out, and the leading whitespaces are stipped from
/// each processed string.
/// For example let's see a section, which contain several
/// integers, which should be inserted into a vector.
/// The functor is implemented as an struct:
/// struct NumberSection {
/// std::vector<int>& _data;
/// NumberSection(std::vector<int>& data) : _data(data) {}
/// void operator()(const std::string& line) {
/// std::istringstream ls(line);
/// while (ls >> value) _data.push_back(value);
/// reader.sectionLines("numbers", NumberSection(vec));
template <typename Functor>
DigraphReader& sectionLines(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::LineSection<Functor>(functor)));
/// \brief Add a section processor with stream oriented reading
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read
/// directly with this function. The first parameter is the type
/// of the section, the second is a functor, which takes an \c
/// std::istream& and an int& parameter, the latter regard to the
/// line number of stream. The functor can read the input while
/// the section go on, and the line number should be modified
template <typename Functor>
DigraphReader& sectionStream(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::StreamSection<Functor>(functor)));
/// \name Using previously constructed node or arc set
/// \brief Use previously constructed node set
/// Use previously constructed node set, and specify the node
DigraphReader& useNodes(const Map& map) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (NodeIt n(_digraph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
/// \brief Use previously constructed node set
/// Use previously constructed node set, and specify the node
/// label map and a functor which converts the label map values to
template <typename Map, typename Converter>
DigraphReader& useNodes(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
for (NodeIt n(_digraph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
/// \brief Use previously constructed arc set
/// Use previously constructed arc set, and specify the arc
DigraphReader& useArcs(const Map& map) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (ArcIt a(_digraph); a != INVALID; ++a) {
_arc_index.insert(std::make_pair(converter(map[a]), a));
/// \brief Use previously constructed arc set
/// Use previously constructed arc set, and specify the arc
/// label map and a functor which converts the label map values to
template <typename Map, typename Converter>
DigraphReader& useArcs(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Arc, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_arcs, "Multiple usage of useArcs() member");
for (ArcIt a(_digraph); a != INVALID; ++a) {
_arc_index.insert(std::make_pair(converter(map[a]), a));
while(++line_num, std::getline(*_is, str)) {
line.clear(); line.str(str);
if (line >> std::ws >> c && c != '#') {
return static_cast<bool>(*_is);
while (readSuccess() && line >> c && c != '@') {
std::vector<int> map_index(_node_maps.size());
int map_num, label_index;
throw DataFormatError("Cannot find map captions");
std::map<std::string, int> maps;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
msg << "Multiple occurence of node map: " << map;
throw DataFormatError(msg.str().c_str());
maps.insert(std::make_pair(map, index));
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_node_maps[i].first);
msg << "Map not found in file: " << _node_maps[i].first;
throw DataFormatError(msg.str().c_str());
map_index[i] = jt->second;
std::map<std::string, int>::iterator jt = maps.find("label");
throw DataFormatError("Label map not found in file");
label_index = jt->second;
while (readLine() && line >> c && c != '@') {
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
_node_index.insert(std::make_pair(tokens[label_index], n));
typename std::map<std::string, Node>::iterator it =
_node_index.find(tokens[label_index]);
if (it == _node_index.end()) {
msg << "Node with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
_node_maps[i].second->set(n, tokens[map_index[i]]);
std::vector<int> map_index(_arc_maps.size());
int map_num, label_index;
throw DataFormatError("Cannot find map captions");
std::map<std::string, int> maps;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
msg << "Multiple occurence of arc map: " << map;
throw DataFormatError(msg.str().c_str());
maps.insert(std::make_pair(map, index));
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_arc_maps[i].first);
msg << "Map not found in file: " << _arc_maps[i].first;
throw DataFormatError(msg.str().c_str());
map_index[i] = jt->second;
std::map<std::string, int>::iterator jt = maps.find("label");
throw DataFormatError("Label map not found in file");
label_index = jt->second;
while (readLine() && line >> c && c != '@') {
std::string source_token;
std::string target_token;
if (!_reader_bits::readToken(line, source_token))
throw DataFormatError("Source not found");
if (!_reader_bits::readToken(line, target_token))
throw DataFormatError("Source not found");
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
typename NodeIndex::iterator it;
it = _node_index.find(source_token);
if (it == _node_index.end()) {
msg << "Item not found: " << source_token;
throw DataFormatError(msg.str().c_str());
Node source = it->second;
it = _node_index.find(target_token);
if (it == _node_index.end()) {
msg << "Item not found: " << target_token;
throw DataFormatError(msg.str().c_str());
Node target = it->second;
a = _digraph.addArc(source, target);
_arc_index.insert(std::make_pair(tokens[label_index], a));
typename std::map<std::string, Arc>::iterator it =
_arc_index.find(tokens[label_index]);
if (it == _arc_index.end()) {
msg << "Arc with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
for (int i = 0; i < static_cast<int>(_arc_maps.size()); ++i) {
_arc_maps[i].second->set(a, tokens[map_index[i]]);
std::set<std::string> read_attr;
while (readLine() && line >> c && c != '@') {
if (!_reader_bits::readToken(line, attr))
throw DataFormatError("Attribute name not found");
if (!_reader_bits::readToken(line, token))
throw DataFormatError("Attribute value not found");
throw DataFormatError("Extra character on the end of line");
std::set<std::string>::iterator it = read_attr.find(attr);
if (it != read_attr.end()) {
msg << "Multiple occurence of attribute " << attr;
throw DataFormatError(msg.str().c_str());
typename Attributes::iterator it = _attributes.lower_bound(attr);
while (it != _attributes.end() && it->first == attr) {
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
if (read_attr.find(it->first) == read_attr.end()) {
msg << "Attribute not found in file: " << it->first;
throw DataFormatError(msg.str().c_str());
/// \name Execution of the reader
/// \brief Start the batch processing
/// This function starts the batch processing
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
throw DataFormatError("Cannot find file");
bool attributes_done = false;
std::set<std::string> extra_sections;
std::string section, caption;
_reader_bits::readToken(line, section);
_reader_bits::readToken(line, caption);
throw DataFormatError("Extra character on the end of line");
if (section == "nodes" && !nodes_done) {
if (_nodes_caption.empty() || _nodes_caption == caption) {
} else if ((section == "arcs" || section == "edges") &&
if (_arcs_caption.empty() || _arcs_caption == caption) {
} else if (section == "attributes" && !attributes_done) {
if (_attributes_caption.empty() || _attributes_caption == caption) {
if (extra_sections.find(section) != extra_sections.end()) {
msg << "Multiple occurence of section " << section;
throw DataFormatError(msg.str().c_str());
Sections::iterator it = _sections.find(section);
if (it != _sections.end()) {
extra_sections.insert(section);
it->second->process(*_is, line_num);
} catch (DataFormatError& error) {
throw DataFormatError("Section @nodes not found");
throw DataFormatError("Section @arcs not found");
if (!attributes_done && !_attributes.empty()) {
throw DataFormatError("Section @attributes not found");
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(std::istream& is, Digraph& digraph) {
DigraphReader<Digraph> tmp(is, digraph);
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(const std::string& fn,
DigraphReader<Digraph> tmp(fn, digraph);
/// \relates DigraphReader
template <typename Digraph>
DigraphReader<Digraph> digraphReader(const char* fn, Digraph& digraph) {
DigraphReader<Digraph> tmp(fn, digraph);
/// \brief LGF reader for undirected graphs
/// This utility reads an \ref lgf-format "LGF" file.
template <typename _Graph>
TEMPLATE_GRAPH_TYPEDEFS(Graph);
std::string _nodes_caption;
std::string _edges_caption;
std::string _attributes_caption;
typedef std::map<std::string, Node> NodeIndex;
typedef std::map<std::string, Edge> EdgeIndex;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Node>*> > NodeMaps;
typedef std::vector<std::pair<std::string,
_reader_bits::MapStorageBase<Edge>*> > EdgeMaps;
typedef std::multimap<std::string, _reader_bits::ValueStorageBase*>
typedef std::map<std::string, _reader_bits::Section*> Sections;
/// Construct a undirected graph reader, which reads from the given
GraphReader(std::istream& is, Graph& graph)
: _is(&is), local_is(false), _graph(graph),
_use_nodes(false), _use_edges(false) {}
/// Construct a undirected graph reader, which reads from the given
GraphReader(const std::string& fn, Graph& graph)
: _is(new std::ifstream(fn.c_str())), local_is(true), _graph(graph),
_use_nodes(false), _use_edges(false) {}
/// Construct a undirected graph reader, which reads from the given
GraphReader(const char* fn, Graph& graph)
: _is(new std::ifstream(fn)), local_is(true), _graph(graph),
_use_nodes(false), _use_edges(false) {}
/// \brief Copy constructor
/// The copy constructor transfers all data from the other reader,
/// therefore the copied reader will not be usable more.
GraphReader(GraphReader& other)
: _is(other._is), local_is(other.local_is), _graph(other._graph),
_use_nodes(other._use_nodes), _use_edges(other._use_edges) {
_node_index.swap(other._node_index);
_edge_index.swap(other._edge_index);
_node_maps.swap(other._node_maps);
_edge_maps.swap(other._edge_maps);
_attributes.swap(other._attributes);
_nodes_caption = other._nodes_caption;
_edges_caption = other._edges_caption;
_attributes_caption = other._attributes_caption;
_sections.swap(other._sections);
for (typename NodeMaps::iterator it = _node_maps.begin();
it != _node_maps.end(); ++it) {
for (typename EdgeMaps::iterator it = _edge_maps.begin();
it != _edge_maps.end(); ++it) {
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
for (typename Sections::iterator it = _sections.begin();
it != _sections.end(); ++it) {
GraphReader& operator=(const GraphReader&);
/// \brief Node map reading rule
/// Add a node map reading rule to the reader.
GraphReader& nodeMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map>(map);
_node_maps.push_back(std::make_pair(caption, storage));
/// \brief Node map reading rule
/// Add a node map reading rule with specialized converter to the
template <typename Map, typename Converter>
GraphReader& nodeMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Node, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Node>* storage =
new _reader_bits::MapStorage<Node, Map, Converter>(map, converter);
_node_maps.push_back(std::make_pair(caption, storage));
/// \brief Edge map reading rule
/// Add an edge map reading rule to the reader.
GraphReader& edgeMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Edge>* storage =
new _reader_bits::MapStorage<Edge, Map>(map);
_edge_maps.push_back(std::make_pair(caption, storage));
/// \brief Edge map reading rule
/// Add an edge map reading rule with specialized converter to the
template <typename Map, typename Converter>
GraphReader& edgeMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Edge, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Edge>* storage =
new _reader_bits::MapStorage<Edge, Map, Converter>(map, converter);
_edge_maps.push_back(std::make_pair(caption, storage));
/// \brief Arc map reading rule
/// Add an arc map reading rule to the reader.
GraphReader& arcMap(const std::string& caption, Map& map) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Edge>* forward_storage =
new _reader_bits::GraphArcMapStorage<Graph, true, Map>(_graph, map);
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
_reader_bits::MapStorageBase<Edge>* backward_storage =
new _reader_bits::GraphArcMapStorage<Graph, false, Map>(_graph, map);
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
/// \brief Arc map reading rule
/// Add an arc map reading rule with specialized converter to the
template <typename Map, typename Converter>
GraphReader& arcMap(const std::string& caption, Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::WriteMap<Arc, typename Map::Value>, Map>();
_reader_bits::MapStorageBase<Edge>* forward_storage =
new _reader_bits::GraphArcMapStorage<Graph, true, Map, Converter>
(_graph, map, converter);
_edge_maps.push_back(std::make_pair('+' + caption, forward_storage));
_reader_bits::MapStorageBase<Edge>* backward_storage =
new _reader_bits::GraphArcMapStorage<Graph, false, Map, Converter>
(_graph, map, converter);
_edge_maps.push_back(std::make_pair('-' + caption, backward_storage));
/// \brief Attribute reading rule
/// Add an attribute reading rule to the reader.
template <typename Value>
GraphReader& attribute(const std::string& caption, Value& value) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value>(value);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Attribute reading rule
/// Add an attribute reading rule with specialized converter to the
template <typename Value, typename Converter>
GraphReader& attribute(const std::string& caption, Value& value,
const Converter& converter = Converter()) {
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Value, Converter>(value, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Node reading rule
/// Add a node reading rule to reader.
GraphReader& node(const std::string& caption, Node& node) {
typedef _reader_bits::MapLookUpConverter<Node> Converter;
Converter converter(_node_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Node, Converter>(node, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Edge reading rule
/// Add an edge reading rule to reader.
GraphReader& edge(const std::string& caption, Edge& edge) {
typedef _reader_bits::MapLookUpConverter<Edge> Converter;
Converter converter(_edge_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Edge, Converter>(edge, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \brief Arc reading rule
/// Add an arc reading rule to reader.
GraphReader& arc(const std::string& caption, Arc& arc) {
typedef _reader_bits::GraphArcLookUpConverter<Graph> Converter;
Converter converter(_graph, _edge_index);
_reader_bits::ValueStorageBase* storage =
new _reader_bits::ValueStorage<Arc, Converter>(arc, converter);
_attributes.insert(std::make_pair(caption, storage));
/// \name Select section by name
/// \brief Set \c \@nodes section to be read
/// Set \c \@nodes section to be read
GraphReader& nodes(const std::string& caption) {
_nodes_caption = caption;
/// \brief Set \c \@edges section to be read
/// Set \c \@edges section to be read
GraphReader& edges(const std::string& caption) {
_edges_caption = caption;
/// \brief Set \c \@attributes section to be read
/// Set \c \@attributes section to be read
GraphReader& attributes(const std::string& caption) {
_attributes_caption = caption;
/// \name Section readers
/// \brief Add a section processor with line oriented reading
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read with
/// this function line by line. The first parameter is the type
/// descriptor of the section, the second is a functor, which
/// takes just one \c std::string parameter. At the reading
/// process, each line of the section will be given to the functor
/// object. However, the empty lines and the comment lines are
/// filtered out, and the leading whitespaces are stipped from
/// each processed string.
/// For example let's see a section, which contain several
/// integers, which should be inserted into a vector.
/// The functor is implemented as an struct:
/// struct NumberSection {
/// std::vector<int>& _data;
/// NumberSection(std::vector<int>& data) : _data(data) {}
/// void operator()(const std::string& line) {
/// std::istringstream ls(line);
/// while (ls >> value) _data.push_back(value);
/// reader.sectionLines("numbers", NumberSection(vec));
template <typename Functor>
GraphReader& sectionLines(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::LineSection<Functor>(functor)));
/// \brief Add a section processor with stream oriented reading
/// In the \e LGF file extra sections can be placed, which contain
/// any data in arbitrary format. These sections can be read
/// directly with this function. The first parameter is the type
/// of the section, the second is a functor, which takes an \c
/// std::istream& and an int& parameter, the latter regard to the
/// line number of stream. The functor can read the input while
/// the section go on, and the line number should be modified
template <typename Functor>
GraphReader& sectionStream(const std::string& type, Functor functor) {
LEMON_ASSERT(!type.empty(), "Type is not empty.");
LEMON_ASSERT(_sections.find(type) == _sections.end(),
"Multiple reading of section.");
LEMON_ASSERT(type != "nodes" && type != "arcs" && type != "edges" &&
type != "attributes", "Multiple reading of section.");
_sections.insert(std::make_pair(type,
new _reader_bits::StreamSection<Functor>(functor)));
/// \name Using previously constructed node or edge set
/// \brief Use previously constructed node set
/// Use previously constructed node set, and specify the node
GraphReader& useNodes(const Map& map) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (NodeIt n(_graph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
/// \brief Use previously constructed node set
/// Use previously constructed node set, and specify the node
/// label map and a functor which converts the label map values to
template <typename Map, typename Converter>
GraphReader& useNodes(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Node, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_nodes, "Multiple usage of useNodes() member");
for (NodeIt n(_graph); n != INVALID; ++n) {
_node_index.insert(std::make_pair(converter(map[n]), n));
/// \brief Use previously constructed edge set
/// Use previously constructed edge set, and specify the edge
GraphReader& useEdges(const Map& map) {
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
_writer_bits::DefaultConverter<typename Map::Value> converter;
for (EdgeIt a(_graph); a != INVALID; ++a) {
_edge_index.insert(std::make_pair(converter(map[a]), a));
/// \brief Use previously constructed edge set
/// Use previously constructed edge set, and specify the edge
/// label map and a functor which converts the label map values to
template <typename Map, typename Converter>
GraphReader& useEdges(const Map& map,
const Converter& converter = Converter()) {
checkConcept<concepts::ReadMap<Edge, typename Map::Value>, Map>();
LEMON_ASSERT(!_use_edges, "Multiple usage of useEdges() member");
for (EdgeIt a(_graph); a != INVALID; ++a) {
_edge_index.insert(std::make_pair(converter(map[a]), a));
while(++line_num, std::getline(*_is, str)) {
line.clear(); line.str(str);
if (line >> std::ws >> c && c != '#') {
return static_cast<bool>(*_is);
while (readSuccess() && line >> c && c != '@') {
std::vector<int> map_index(_node_maps.size());
int map_num, label_index;
throw DataFormatError("Cannot find map captions");
std::map<std::string, int> maps;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
msg << "Multiple occurence of node map: " << map;
throw DataFormatError(msg.str().c_str());
maps.insert(std::make_pair(map, index));
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_node_maps[i].first);
msg << "Map not found in file: " << _node_maps[i].first;
throw DataFormatError(msg.str().c_str());
map_index[i] = jt->second;
std::map<std::string, int>::iterator jt = maps.find("label");
throw DataFormatError("Label map not found in file");
label_index = jt->second;
while (readLine() && line >> c && c != '@') {
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
_node_index.insert(std::make_pair(tokens[label_index], n));
typename std::map<std::string, Node>::iterator it =
_node_index.find(tokens[label_index]);
if (it == _node_index.end()) {
msg << "Node with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
for (int i = 0; i < static_cast<int>(_node_maps.size()); ++i) {
_node_maps[i].second->set(n, tokens[map_index[i]]);
std::vector<int> map_index(_edge_maps.size());
int map_num, label_index;
throw DataFormatError("Cannot find map captions");
std::map<std::string, int> maps;
while (_reader_bits::readToken(line, map)) {
if (maps.find(map) != maps.end()) {
msg << "Multiple occurence of edge map: " << map;
throw DataFormatError(msg.str().c_str());
maps.insert(std::make_pair(map, index));
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
std::map<std::string, int>::iterator jt =
maps.find(_edge_maps[i].first);
msg << "Map not found in file: " << _edge_maps[i].first;
throw DataFormatError(msg.str().c_str());
map_index[i] = jt->second;
std::map<std::string, int>::iterator jt = maps.find("label");
throw DataFormatError("Label map not found in file");
label_index = jt->second;
while (readLine() && line >> c && c != '@') {
std::string source_token;
std::string target_token;
if (!_reader_bits::readToken(line, source_token))
throw DataFormatError("Source not found");
if (!_reader_bits::readToken(line, target_token))
throw DataFormatError("Source not found");
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
if (!_reader_bits::readToken(line, tokens[i])) {
msg << "Column not found (" << i + 1 << ")";
throw DataFormatError(msg.str().c_str());
if (line >> std::ws >> c)
throw DataFormatError("Extra character on the end of line");
typename NodeIndex::iterator it;
it = _node_index.find(source_token);
if (it == _node_index.end()) {
msg << "Item not found: " << source_token;
throw DataFormatError(msg.str().c_str());
Node source = it->second;
it = _node_index.find(target_token);
if (it == _node_index.end()) {
msg << "Item not found: " << target_token;
throw DataFormatError(msg.str().c_str());
Node target = it->second;
e = _graph.addEdge(source, target);
_edge_index.insert(std::make_pair(tokens[label_index], e));
typename std::map<std::string, Edge>::iterator it =
_edge_index.find(tokens[label_index]);
if (it == _edge_index.end()) {
msg << "Edge with label not found: " << tokens[label_index];
throw DataFormatError(msg.str().c_str());
for (int i = 0; i < static_cast<int>(_edge_maps.size()); ++i) {
_edge_maps[i].second->set(e, tokens[map_index[i]]);
std::set<std::string> read_attr;
while (readLine() && line >> c && c != '@') {
if (!_reader_bits::readToken(line, attr))
throw DataFormatError("Attribute name not found");
if (!_reader_bits::readToken(line, token))
throw DataFormatError("Attribute value not found");
throw DataFormatError("Extra character on the end of line");
std::set<std::string>::iterator it = read_attr.find(attr);
if (it != read_attr.end()) {
msg << "Multiple occurence of attribute " << attr;
throw DataFormatError(msg.str().c_str());
typename Attributes::iterator it = _attributes.lower_bound(attr);
while (it != _attributes.end() && it->first == attr) {
for (typename Attributes::iterator it = _attributes.begin();
it != _attributes.end(); ++it) {
if (read_attr.find(it->first) == read_attr.end()) {
msg << "Attribute not found in file: " << it->first;
throw DataFormatError(msg.str().c_str());
/// \name Execution of the reader
/// \brief Start the batch processing
/// This function starts the batch processing
LEMON_ASSERT(_is != 0, "This reader assigned to an other reader");
bool attributes_done = false;
std::set<std::string> extra_sections;
std::string section, caption;
_reader_bits::readToken(line, section);
_reader_bits::readToken(line, caption);
throw DataFormatError("Extra character on the end of line");
if (section == "nodes" && !nodes_done) {
if (_nodes_caption.empty() || _nodes_caption == caption) {
} else if ((section == "edges" || section == "arcs") &&
if (_edges_caption.empty() || _edges_caption == caption) {
} else if (section == "attributes" && !attributes_done) {
if (_attributes_caption.empty() || _attributes_caption == caption) {
if (extra_sections.find(section) != extra_sections.end()) {
msg << "Multiple occurence of section " << section;
throw DataFormatError(msg.str().c_str());
Sections::iterator it = _sections.find(section);
if (it != _sections.end()) {
extra_sections.insert(section);
it->second->process(*_is, line_num);
} catch (DataFormatError& error) {
throw DataFormatError("Section @nodes not found");
throw DataFormatError("Section @edges not found");
if (!attributes_done && !_attributes.empty()) {
throw DataFormatError("Section @attributes not found");
template <typename Graph>
GraphReader<Graph> graphReader(std::istream& is, Graph& graph) {
GraphReader<Graph> tmp(is, graph);
template <typename Graph>
GraphReader<Graph> graphReader(const std::string& fn,
GraphReader<Graph> tmp(fn, graph);
template <typename Graph>
GraphReader<Graph> graphReader(const char* fn, Graph& graph) {
GraphReader<Graph> tmp(fn, graph);