diff -r 4317d277ba21 -r 765619b7cbb2 lemon/lgf_writer.h --- a/lemon/lgf_writer.h Sun Jul 13 16:46:56 2008 +0100 +++ b/lemon/lgf_writer.h Sun Jul 13 19:51:02 2008 +0100 @@ -1,6 +1,6 @@ -/* -*- C++ -*- +/* -*- mode: C++; indent-tabs-mode: nil; -*- * - * This file is a part of LEMON, a generic C++ optimization library + * This file is a part of LEMON, a generic C++ optimization library. * * Copyright (C) 2003-2008 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport @@ -43,9 +43,9 @@ template struct DefaultConverter { std::string operator()(const Value& value) { - std::ostringstream os; - os << value; - return os.str(); + std::ostringstream os; + os << value; + return os.str(); } }; @@ -62,12 +62,12 @@ private: const Map& _map; - + public: MapLess(const Map& map) : _map(map) {} bool operator()(const Item& left, const Item& right) { - return _map[left] < _map[right]; + return _map[left] < _map[right]; } }; @@ -81,18 +81,18 @@ private: const Graph& _graph; const Map& _map; - + public: - GraphArcMapLess(const Graph& graph, const Map& map) - : _graph(graph), _map(map) {} + GraphArcMapLess(const Graph& graph, const Map& map) + : _graph(graph), _map(map) {} bool operator()(const Item& left, const Item& right) { - return _map[_graph.direct(left, _dir)] < - _map[_graph.direct(right, _dir)]; + return _map[_graph.direct(left, _dir)] < + _map[_graph.direct(right, _dir)]; } }; - template + template class MapStorageBase { public: typedef _Item Item; @@ -105,34 +105,34 @@ virtual void sort(std::vector&) = 0; }; - template > + template > class MapStorage : public MapStorageBase<_Item> { public: typedef _Map Map; typedef _Converter Converter; typedef _Item Item; - + private: const Map& _map; Converter _converter; public: - MapStorage(const Map& map, const Converter& converter = Converter()) - : _map(map), _converter(converter) {} + MapStorage(const Map& map, const Converter& converter = Converter()) + : _map(map), _converter(converter) {} virtual ~MapStorage() {} virtual std::string get(const Item& item) { - return _converter(_map[item]); + return _converter(_map[item]); } virtual void sort(std::vector& items) { - MapLess less(_map); - std::sort(items.begin(), items.end(), less); + MapLess less(_map); + std::sort(items.begin(), items.end(), less); } }; - template > + template > class GraphArcMapStorage : public MapStorageBase { public: typedef _Map Map; @@ -140,24 +140,24 @@ typedef _Graph Graph; typedef typename Graph::Edge Item; static const bool dir = _dir; - + private: const Graph& _graph; const Map& _map; Converter _converter; public: - GraphArcMapStorage(const Graph& graph, const Map& map, - const Converter& converter = Converter()) - : _graph(graph), _map(map), _converter(converter) {} + GraphArcMapStorage(const Graph& graph, const Map& map, + const Converter& converter = Converter()) + : _graph(graph), _map(map), _converter(converter) {} virtual ~GraphArcMapStorage() {} virtual std::string get(const Item& item) { - return _converter(_map[_graph.direct(item, dir)]); + return _converter(_map[_graph.direct(item, dir)]); } virtual void sort(std::vector& items) { - GraphArcMapLess less(_graph, _map); - std::sort(items.begin(), items.end(), less); + GraphArcMapLess less(_graph, _map); + std::sort(items.begin(), items.end(), less); } }; @@ -166,7 +166,7 @@ ValueStorageBase() {} virtual ~ValueStorageBase() {} - virtual std::string get() = 0; + virtual std::string get() = 0; }; template > @@ -181,27 +181,27 @@ public: ValueStorage(const Value& value, const Converter& converter = Converter()) - : _value(value), _converter(converter) {} + : _value(value), _converter(converter) {} virtual std::string get() { - return _converter(_value); + return _converter(_value); } }; template struct MapLookUpConverter { const std::map& _map; - - MapLookUpConverter(const std::map& map) - : _map(map) {} - + + MapLookUpConverter(const std::map& map) + : _map(map) {} + std::string operator()(const Value& str) { - typename std::map::const_iterator it = - _map.find(str); - if (it == _map.end()) { - throw DataFormatError("Item not found"); - } - return it->second; + typename std::map::const_iterator it = + _map.find(str); + if (it == _map.end()) { + throw DataFormatError("Item not found"); + } + return it->second; } }; @@ -209,71 +209,71 @@ struct GraphArcLookUpConverter { const Graph& _graph; const std::map& _map; - - GraphArcLookUpConverter(const Graph& graph, - const std::map& map) - : _graph(graph), _map(map) {} - + + GraphArcLookUpConverter(const Graph& graph, + const std::map& map) + : _graph(graph), _map(map) {} + std::string operator()(const typename Graph::Arc& val) { - typename std::map - ::const_iterator it = _map.find(val); - if (it == _map.end()) { - throw DataFormatError("Item not found"); - } - return (_graph.direction(val) ? '+' : '-') + it->second; + typename std::map + ::const_iterator it = _map.find(val); + if (it == _map.end()) { + throw DataFormatError("Item not found"); + } + return (_graph.direction(val) ? '+' : '-') + it->second; } }; inline bool isWhiteSpace(char c) { - return c == ' ' || c == '\t' || c == '\v' || - c == '\n' || c == '\r' || c == '\f'; + return c == ' ' || c == '\t' || c == '\v' || + c == '\n' || c == '\r' || c == '\f'; } inline bool isEscaped(char c) { - return c == '\\' || c == '\"' || c == '\'' || - c == '\a' || c == '\b'; + return c == '\\' || c == '\"' || c == '\'' || + c == '\a' || c == '\b'; } inline static void writeEscape(std::ostream& os, char c) { switch (c) { case '\\': - os << "\\\\"; - return; + os << "\\\\"; + return; case '\"': - os << "\\\""; - return; + os << "\\\""; + return; case '\a': - os << "\\a"; - return; + os << "\\a"; + return; case '\b': - os << "\\b"; - return; + os << "\\b"; + return; case '\f': - os << "\\f"; - return; + os << "\\f"; + return; case '\r': - os << "\\r"; - return; + os << "\\r"; + return; case '\n': - os << "\\n"; - return; + os << "\\n"; + return; case '\t': - os << "\\t"; - return; + os << "\\t"; + return; case '\v': - os << "\\v"; - return; + os << "\\v"; + return; default: - if (c < 0x20) { - std::ios::fmtflags flags = os.flags(); - os << '\\' << std::oct << static_cast(c); - os.flags(flags); - } else { - os << c; - } - return; - } + if (c < 0x20) { + std::ios::fmtflags flags = os.flags(); + os << '\\' << std::oct << static_cast(c); + os.flags(flags); + } else { + os << c; + } + return; + } } inline bool requireEscape(const std::string& str) { @@ -281,24 +281,24 @@ std::istringstream is(str); char c; while (is.get(c)) { - if (isWhiteSpace(c) || isEscaped(c)) { - return true; - } + if (isWhiteSpace(c) || isEscaped(c)) { + return true; + } } return false; } - + inline std::ostream& writeToken(std::ostream& os, const std::string& str) { if (requireEscape(str)) { - os << '\"'; - for (std::string::const_iterator it = str.begin(); - it != str.end(); ++it) { - writeEscape(os, *it); - } - os << '\"'; + os << '\"'; + for (std::string::const_iterator it = str.begin(); + it != str.end(); ++it) { + writeEscape(os, *it); + } + os << '\"'; } else { - os << str; + os << str; } return os; } @@ -309,19 +309,19 @@ class DigraphWriter; template - DigraphWriter digraphWriter(std::ostream& os, - const Digraph& digraph); + DigraphWriter digraphWriter(std::ostream& os, + const Digraph& digraph); template - DigraphWriter digraphWriter(const std::string& fn, - const Digraph& digraph); + DigraphWriter digraphWriter(const std::string& fn, + const Digraph& digraph); template - DigraphWriter digraphWriter(const char *fn, - const Digraph& digraph); - + DigraphWriter digraphWriter(const char *fn, + const Digraph& digraph); + /// \ingroup lemon_io - /// + /// /// \brief \ref lgf-format "LGF" writer for directed graphs /// /// This utility writes an \ref lgf-format "LGF" file. @@ -370,7 +370,7 @@ typedef _Digraph Digraph; TEMPLATE_DIGRAPH_TYPEDEFS(Digraph); - + private: @@ -382,21 +382,21 @@ std::string _nodes_caption; std::string _arcs_caption; std::string _attributes_caption; - + typedef std::map NodeIndex; NodeIndex _node_index; typedef std::map ArcIndex; ArcIndex _arc_index; - typedef std::vector* > > NodeMaps; - NodeMaps _node_maps; + typedef std::vector* > > NodeMaps; + NodeMaps _node_maps; - typedef std::vector* > >ArcMaps; ArcMaps _arc_maps; - typedef std::vector > Attributes; Attributes _attributes; @@ -409,60 +409,60 @@ /// /// Construct a directed graph writer, which writes to the given /// output stream. - DigraphWriter(std::ostream& is, const Digraph& digraph) + DigraphWriter(std::ostream& is, const Digraph& digraph) : _os(&is), local_os(false), _digraph(digraph), - _skip_nodes(false), _skip_arcs(false) {} + _skip_nodes(false), _skip_arcs(false) {} /// \brief Constructor /// /// Construct a directed graph writer, which writes to the given /// output file. - DigraphWriter(const std::string& fn, const Digraph& digraph) + DigraphWriter(const std::string& fn, const Digraph& digraph) : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph), - _skip_nodes(false), _skip_arcs(false) {} + _skip_nodes(false), _skip_arcs(false) {} /// \brief Constructor /// /// Construct a directed graph writer, which writes to the given /// output file. - DigraphWriter(const char* fn, const Digraph& digraph) + DigraphWriter(const char* fn, const Digraph& digraph) : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph), - _skip_nodes(false), _skip_arcs(false) {} + _skip_nodes(false), _skip_arcs(false) {} /// \brief Destructor ~DigraphWriter() { - for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - delete it->second; + for (typename NodeMaps::iterator it = _node_maps.begin(); + it != _node_maps.end(); ++it) { + delete it->second; } - for (typename ArcMaps::iterator it = _arc_maps.begin(); - it != _arc_maps.end(); ++it) { - delete it->second; + for (typename ArcMaps::iterator it = _arc_maps.begin(); + it != _arc_maps.end(); ++it) { + delete it->second; } - for (typename Attributes::iterator it = _attributes.begin(); - it != _attributes.end(); ++it) { - delete it->second; + for (typename Attributes::iterator it = _attributes.begin(); + it != _attributes.end(); ++it) { + delete it->second; } if (local_os) { - delete _os; + delete _os; } } private: - friend DigraphWriter digraphWriter<>(std::ostream& os, - const Digraph& digraph); - friend DigraphWriter digraphWriter<>(const std::string& fn, - const Digraph& digraph); - friend DigraphWriter digraphWriter<>(const char *fn, - const Digraph& digraph); + friend DigraphWriter digraphWriter<>(std::ostream& os, + const Digraph& digraph); + friend DigraphWriter digraphWriter<>(const std::string& fn, + const Digraph& digraph); + friend DigraphWriter digraphWriter<>(const char *fn, + const Digraph& digraph); - DigraphWriter(DigraphWriter& other) + DigraphWriter(DigraphWriter& other) : _os(other._os), local_os(other.local_os), _digraph(other._digraph), - _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { + _skip_nodes(other._skip_nodes), _skip_arcs(other._skip_arcs) { other._os = 0; other.local_os = false; @@ -478,22 +478,22 @@ _arcs_caption = other._arcs_caption; _attributes_caption = other._attributes_caption; } - + DigraphWriter& operator=(const DigraphWriter&); public: /// \name Writing rules /// @{ - + /// \brief Node map writing rule /// /// Add a node map writing rule to the writer. template DigraphWriter& nodeMap(const std::string& caption, const Map& map) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map); _node_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -503,11 +503,11 @@ /// Add a node map writing rule with specialized converter to the /// writer. template - DigraphWriter& nodeMap(const std::string& caption, const Map& map, - const Converter& converter = Converter()) { + DigraphWriter& nodeMap(const std::string& caption, const Map& map, + const Converter& converter = Converter()) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map, converter); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map, converter); _node_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -518,8 +518,8 @@ template DigraphWriter& arcMap(const std::string& caption, const Map& map) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map); _arc_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -529,11 +529,11 @@ /// Add an arc map writing rule with specialized converter to the /// writer. template - DigraphWriter& arcMap(const std::string& caption, const Map& map, - const Converter& converter = Converter()) { + DigraphWriter& arcMap(const std::string& caption, const Map& map, + const Converter& converter = Converter()) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map, converter); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map, converter); _arc_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -543,8 +543,8 @@ /// Add an attribute writing rule to the writer. template DigraphWriter& attribute(const std::string& caption, const Value& value) { - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(value); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(value); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -554,10 +554,10 @@ /// Add an attribute writing rule with specialized converter to the /// writer. template - DigraphWriter& attribute(const std::string& caption, const Value& value, - const Converter& converter = Converter()) { - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(value, converter); + DigraphWriter& attribute(const std::string& caption, const Value& value, + const Converter& converter = Converter()) { + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(value, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -568,8 +568,8 @@ DigraphWriter& node(const std::string& caption, const Node& node) { typedef _writer_bits::MapLookUpConverter Converter; Converter converter(_node_index); - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(node, converter); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(node, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -580,8 +580,8 @@ DigraphWriter& arc(const std::string& caption, const Arc& arc) { typedef _writer_bits::MapLookUpConverter Converter; Converter converter(_arc_index); - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(arc, converter); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(arc, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -641,175 +641,175 @@ void writeNodes() { _writer_bits::MapStorageBase* label = 0; for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { + it != _node_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } *_os << "@nodes"; if (!_nodes_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _nodes_caption); + _writer_bits::writeToken(*_os << ' ', _nodes_caption); } *_os << std::endl; if (label == 0) { - *_os << "label" << '\t'; + *_os << "label" << '\t'; } for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << '\t'; + it != _node_maps.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << '\t'; } *_os << std::endl; std::vector nodes; for (NodeIt n(_digraph); n != INVALID; ++n) { - nodes.push_back(n); + nodes.push_back(n); } - + if (label == 0) { - IdMap id_map(_digraph); - _writer_bits::MapLess > id_less(id_map); - std::sort(nodes.begin(), nodes.end(), id_less); + IdMap id_map(_digraph); + _writer_bits::MapLess > id_less(id_map); + std::sort(nodes.begin(), nodes.end(), id_less); } else { - label->sort(nodes); + label->sort(nodes); } for (int i = 0; i < static_cast(nodes.size()); ++i) { - Node n = nodes[i]; - if (label == 0) { - std::ostringstream os; - os << _digraph.id(n); - _writer_bits::writeToken(*_os, os.str()); - *_os << '\t'; - _node_index.insert(std::make_pair(n, os.str())); - } - for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - std::string value = it->second->get(n); - _writer_bits::writeToken(*_os, value); - if (it->first == "label") { - _node_index.insert(std::make_pair(n, value)); - } - *_os << '\t'; - } - *_os << std::endl; + Node n = nodes[i]; + if (label == 0) { + std::ostringstream os; + os << _digraph.id(n); + _writer_bits::writeToken(*_os, os.str()); + *_os << '\t'; + _node_index.insert(std::make_pair(n, os.str())); + } + for (typename NodeMaps::iterator it = _node_maps.begin(); + it != _node_maps.end(); ++it) { + std::string value = it->second->get(n); + _writer_bits::writeToken(*_os, value); + if (it->first == "label") { + _node_index.insert(std::make_pair(n, value)); + } + *_os << '\t'; + } + *_os << std::endl; } } void createNodeIndex() { _writer_bits::MapStorageBase* label = 0; for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { + it != _node_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } if (label == 0) { - for (NodeIt n(_digraph); n != INVALID; ++n) { - std::ostringstream os; - os << _digraph.id(n); - _node_index.insert(std::make_pair(n, os.str())); - } + for (NodeIt n(_digraph); n != INVALID; ++n) { + std::ostringstream os; + os << _digraph.id(n); + _node_index.insert(std::make_pair(n, os.str())); + } } else { - for (NodeIt n(_digraph); n != INVALID; ++n) { - std::string value = label->get(n); - _node_index.insert(std::make_pair(n, value)); - } + for (NodeIt n(_digraph); n != INVALID; ++n) { + std::string value = label->get(n); + _node_index.insert(std::make_pair(n, value)); + } } } void writeArcs() { _writer_bits::MapStorageBase* label = 0; for (typename ArcMaps::iterator it = _arc_maps.begin(); - it != _arc_maps.end(); ++it) { + it != _arc_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } *_os << "@arcs"; if (!_arcs_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _arcs_caption); + _writer_bits::writeToken(*_os << ' ', _arcs_caption); } *_os << std::endl; *_os << '\t' << '\t'; if (label == 0) { - *_os << "label" << '\t'; + *_os << "label" << '\t'; } for (typename ArcMaps::iterator it = _arc_maps.begin(); - it != _arc_maps.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << '\t'; + it != _arc_maps.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << '\t'; } *_os << std::endl; std::vector arcs; for (ArcIt n(_digraph); n != INVALID; ++n) { - arcs.push_back(n); + arcs.push_back(n); } - + if (label == 0) { - IdMap id_map(_digraph); - _writer_bits::MapLess > id_less(id_map); - std::sort(arcs.begin(), arcs.end(), id_less); + IdMap id_map(_digraph); + _writer_bits::MapLess > id_less(id_map); + std::sort(arcs.begin(), arcs.end(), id_less); } else { - label->sort(arcs); + label->sort(arcs); } for (int i = 0; i < static_cast(arcs.size()); ++i) { - Arc a = arcs[i]; - _writer_bits::writeToken(*_os, _node_index. - find(_digraph.source(a))->second); - *_os << '\t'; - _writer_bits::writeToken(*_os, _node_index. - find(_digraph.target(a))->second); - *_os << '\t'; - if (label == 0) { - std::ostringstream os; - os << _digraph.id(a); - _writer_bits::writeToken(*_os, os.str()); - *_os << '\t'; - _arc_index.insert(std::make_pair(a, os.str())); - } - for (typename ArcMaps::iterator it = _arc_maps.begin(); - it != _arc_maps.end(); ++it) { - std::string value = it->second->get(a); - _writer_bits::writeToken(*_os, value); - if (it->first == "label") { - _arc_index.insert(std::make_pair(a, value)); - } - *_os << '\t'; - } - *_os << std::endl; + Arc a = arcs[i]; + _writer_bits::writeToken(*_os, _node_index. + find(_digraph.source(a))->second); + *_os << '\t'; + _writer_bits::writeToken(*_os, _node_index. + find(_digraph.target(a))->second); + *_os << '\t'; + if (label == 0) { + std::ostringstream os; + os << _digraph.id(a); + _writer_bits::writeToken(*_os, os.str()); + *_os << '\t'; + _arc_index.insert(std::make_pair(a, os.str())); + } + for (typename ArcMaps::iterator it = _arc_maps.begin(); + it != _arc_maps.end(); ++it) { + std::string value = it->second->get(a); + _writer_bits::writeToken(*_os, value); + if (it->first == "label") { + _arc_index.insert(std::make_pair(a, value)); + } + *_os << '\t'; + } + *_os << std::endl; } } void createArcIndex() { _writer_bits::MapStorageBase* label = 0; for (typename ArcMaps::iterator it = _arc_maps.begin(); - it != _arc_maps.end(); ++it) { + it != _arc_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } if (label == 0) { - for (ArcIt a(_digraph); a != INVALID; ++a) { - std::ostringstream os; - os << _digraph.id(a); - _arc_index.insert(std::make_pair(a, os.str())); - } + for (ArcIt a(_digraph); a != INVALID; ++a) { + std::ostringstream os; + os << _digraph.id(a); + _arc_index.insert(std::make_pair(a, os.str())); + } } else { - for (ArcIt a(_digraph); a != INVALID; ++a) { - std::string value = label->get(a); - _arc_index.insert(std::make_pair(a, value)); - } + for (ArcIt a(_digraph); a != INVALID; ++a) { + std::string value = label->get(a); + _arc_index.insert(std::make_pair(a, value)); + } } } @@ -817,20 +817,20 @@ if (_attributes.empty()) return; *_os << "@attributes"; if (!_attributes_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _attributes_caption); + _writer_bits::writeToken(*_os << ' ', _attributes_caption); } *_os << std::endl; for (typename Attributes::iterator it = _attributes.begin(); - it != _attributes.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << ' '; - _writer_bits::writeToken(*_os, it->second->get()); - *_os << std::endl; + it != _attributes.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << ' '; + _writer_bits::writeToken(*_os, it->second->get()); + *_os << std::endl; } } - + public: - - /// \name Execution of the writer + + /// \name Execution of the writer /// @{ /// \brief Start the batch processing @@ -838,14 +838,14 @@ /// This function starts the batch processing. void run() { if (!_skip_nodes) { - writeNodes(); + writeNodes(); } else { - createNodeIndex(); + createNodeIndex(); } - if (!_skip_arcs) { - writeArcs(); + if (!_skip_arcs) { + writeArcs(); } else { - createArcIndex(); + createArcIndex(); } writeAttributes(); } @@ -861,34 +861,34 @@ }; /// \brief Return a \ref DigraphWriter class - /// + /// /// This function just returns a \ref DigraphWriter class. /// \relates DigraphWriter template - DigraphWriter digraphWriter(std::ostream& os, - const Digraph& digraph) { + DigraphWriter digraphWriter(std::ostream& os, + const Digraph& digraph) { DigraphWriter tmp(os, digraph); return tmp; } /// \brief Return a \ref DigraphWriter class - /// + /// /// This function just returns a \ref DigraphWriter class. /// \relates DigraphWriter template - DigraphWriter digraphWriter(const std::string& fn, - const Digraph& digraph) { + DigraphWriter digraphWriter(const std::string& fn, + const Digraph& digraph) { DigraphWriter tmp(fn, digraph); return tmp; } /// \brief Return a \ref DigraphWriter class - /// + /// /// This function just returns a \ref DigraphWriter class. /// \relates DigraphWriter template - DigraphWriter digraphWriter(const char* fn, - const Digraph& digraph) { + DigraphWriter digraphWriter(const char* fn, + const Digraph& digraph) { DigraphWriter tmp(fn, digraph); return tmp; } @@ -897,16 +897,16 @@ class GraphWriter; template - GraphWriter graphWriter(std::ostream& os, const Graph& graph); + GraphWriter graphWriter(std::ostream& os, const Graph& graph); template - GraphWriter graphWriter(const std::string& fn, const Graph& graph); + GraphWriter graphWriter(const std::string& fn, const Graph& graph); template - GraphWriter graphWriter(const char *fn, const Graph& graph); + GraphWriter graphWriter(const char *fn, const Graph& graph); /// \ingroup lemon_io - /// + /// /// \brief \ref lgf-format "LGF" writer for directed graphs /// /// This utility writes an \ref lgf-format "LGF" file. @@ -926,7 +926,7 @@ typedef _Graph Graph; TEMPLATE_GRAPH_TYPEDEFS(Graph); - + private: @@ -938,21 +938,21 @@ std::string _nodes_caption; std::string _edges_caption; std::string _attributes_caption; - + typedef std::map NodeIndex; NodeIndex _node_index; typedef std::map EdgeIndex; EdgeIndex _edge_index; - typedef std::vector* > > NodeMaps; - NodeMaps _node_maps; + typedef std::vector* > > NodeMaps; + NodeMaps _node_maps; - typedef std::vector* > >EdgeMaps; EdgeMaps _edge_maps; - typedef std::vector > Attributes; Attributes _attributes; @@ -965,60 +965,60 @@ /// /// Construct a directed graph writer, which writes to the given /// output stream. - GraphWriter(std::ostream& is, const Graph& graph) + GraphWriter(std::ostream& is, const Graph& graph) : _os(&is), local_os(false), _graph(graph), - _skip_nodes(false), _skip_edges(false) {} + _skip_nodes(false), _skip_edges(false) {} /// \brief Constructor /// /// Construct a directed graph writer, which writes to the given /// output file. - GraphWriter(const std::string& fn, const Graph& graph) + GraphWriter(const std::string& fn, const Graph& graph) : _os(new std::ofstream(fn.c_str())), local_os(true), _graph(graph), - _skip_nodes(false), _skip_edges(false) {} + _skip_nodes(false), _skip_edges(false) {} /// \brief Constructor /// /// Construct a directed graph writer, which writes to the given /// output file. - GraphWriter(const char* fn, const Graph& graph) + GraphWriter(const char* fn, const Graph& graph) : _os(new std::ofstream(fn)), local_os(true), _graph(graph), - _skip_nodes(false), _skip_edges(false) {} + _skip_nodes(false), _skip_edges(false) {} /// \brief Destructor ~GraphWriter() { - for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - delete it->second; + for (typename NodeMaps::iterator it = _node_maps.begin(); + it != _node_maps.end(); ++it) { + delete it->second; } - for (typename EdgeMaps::iterator it = _edge_maps.begin(); - it != _edge_maps.end(); ++it) { - delete it->second; + for (typename EdgeMaps::iterator it = _edge_maps.begin(); + it != _edge_maps.end(); ++it) { + delete it->second; } - for (typename Attributes::iterator it = _attributes.begin(); - it != _attributes.end(); ++it) { - delete it->second; + for (typename Attributes::iterator it = _attributes.begin(); + it != _attributes.end(); ++it) { + delete it->second; } if (local_os) { - delete _os; + delete _os; } } - + private: - friend GraphWriter graphWriter<>(std::ostream& os, - const Graph& graph); - friend GraphWriter graphWriter<>(const std::string& fn, - const Graph& graph); - friend GraphWriter graphWriter<>(const char *fn, - const Graph& graph); + friend GraphWriter graphWriter<>(std::ostream& os, + const Graph& graph); + friend GraphWriter graphWriter<>(const std::string& fn, + const Graph& graph); + friend GraphWriter graphWriter<>(const char *fn, + const Graph& graph); - GraphWriter(GraphWriter& other) + GraphWriter(GraphWriter& other) : _os(other._os), local_os(other.local_os), _graph(other._graph), - _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { + _skip_nodes(other._skip_nodes), _skip_edges(other._skip_edges) { other._os = 0; other.local_os = false; @@ -1041,15 +1041,15 @@ /// \name Writing rules /// @{ - + /// \brief Node map writing rule /// /// Add a node map writing rule to the writer. template GraphWriter& nodeMap(const std::string& caption, const Map& map) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map); _node_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -1059,11 +1059,11 @@ /// Add a node map writing rule with specialized converter to the /// writer. template - GraphWriter& nodeMap(const std::string& caption, const Map& map, - const Converter& converter = Converter()) { + GraphWriter& nodeMap(const std::string& caption, const Map& map, + const Converter& converter = Converter()) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map, converter); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map, converter); _node_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -1074,8 +1074,8 @@ template GraphWriter& edgeMap(const std::string& caption, const Map& map) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map); _edge_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -1085,11 +1085,11 @@ /// Add an edge map writing rule with specialized converter to the /// writer. template - GraphWriter& edgeMap(const std::string& caption, const Map& map, - const Converter& converter = Converter()) { + GraphWriter& edgeMap(const std::string& caption, const Map& map, + const Converter& converter = Converter()) { checkConcept, Map>(); - _writer_bits::MapStorageBase* storage = - new _writer_bits::MapStorage(map, converter); + _writer_bits::MapStorageBase* storage = + new _writer_bits::MapStorage(map, converter); _edge_maps.push_back(std::make_pair(caption, storage)); return *this; } @@ -1100,11 +1100,11 @@ template GraphWriter& arcMap(const std::string& caption, const Map& map) { checkConcept, Map>(); - _writer_bits::MapStorageBase* forward_storage = - new _writer_bits::GraphArcMapStorage(_graph, map); + _writer_bits::MapStorageBase* forward_storage = + new _writer_bits::GraphArcMapStorage(_graph, map); _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); - _writer_bits::MapStorageBase* backward_storage = - new _writer_bits::GraphArcMapStorage(_graph, map); + _writer_bits::MapStorageBase* backward_storage = + new _writer_bits::GraphArcMapStorage(_graph, map); _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); return *this; } @@ -1114,16 +1114,16 @@ /// Add an arc map writing rule with specialized converter to the /// writer. template - GraphWriter& arcMap(const std::string& caption, const Map& map, - const Converter& converter = Converter()) { + GraphWriter& arcMap(const std::string& caption, const Map& map, + const Converter& converter = Converter()) { checkConcept, Map>(); - _writer_bits::MapStorageBase* forward_storage = - new _writer_bits::GraphArcMapStorage - (_graph, map, converter); + _writer_bits::MapStorageBase* forward_storage = + new _writer_bits::GraphArcMapStorage + (_graph, map, converter); _edge_maps.push_back(std::make_pair('+' + caption, forward_storage)); - _writer_bits::MapStorageBase* backward_storage = - new _writer_bits::GraphArcMapStorage - (_graph, map, converter); + _writer_bits::MapStorageBase* backward_storage = + new _writer_bits::GraphArcMapStorage + (_graph, map, converter); _edge_maps.push_back(std::make_pair('-' + caption, backward_storage)); return *this; } @@ -1133,8 +1133,8 @@ /// Add an attribute writing rule to the writer. template GraphWriter& attribute(const std::string& caption, const Value& value) { - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(value); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(value); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -1144,10 +1144,10 @@ /// Add an attribute writing rule with specialized converter to the /// writer. template - GraphWriter& attribute(const std::string& caption, const Value& value, - const Converter& converter = Converter()) { - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(value, converter); + GraphWriter& attribute(const std::string& caption, const Value& value, + const Converter& converter = Converter()) { + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(value, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -1158,8 +1158,8 @@ GraphWriter& node(const std::string& caption, const Node& node) { typedef _writer_bits::MapLookUpConverter Converter; Converter converter(_node_index); - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(node, converter); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(node, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -1170,8 +1170,8 @@ GraphWriter& edge(const std::string& caption, const Edge& edge) { typedef _writer_bits::MapLookUpConverter Converter; Converter converter(_edge_index); - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(edge, converter); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(edge, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -1182,8 +1182,8 @@ GraphWriter& arc(const std::string& caption, const Arc& arc) { typedef _writer_bits::GraphArcLookUpConverter Converter; Converter converter(_graph, _edge_index); - _writer_bits::ValueStorageBase* storage = - new _writer_bits::ValueStorage(arc, converter); + _writer_bits::ValueStorageBase* storage = + new _writer_bits::ValueStorage(arc, converter); _attributes.push_back(std::make_pair(caption, storage)); return *this; } @@ -1243,175 +1243,175 @@ void writeNodes() { _writer_bits::MapStorageBase* label = 0; for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { + it != _node_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } *_os << "@nodes"; if (!_nodes_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _nodes_caption); + _writer_bits::writeToken(*_os << ' ', _nodes_caption); } *_os << std::endl; if (label == 0) { - *_os << "label" << '\t'; + *_os << "label" << '\t'; } for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << '\t'; + it != _node_maps.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << '\t'; } *_os << std::endl; std::vector nodes; for (NodeIt n(_graph); n != INVALID; ++n) { - nodes.push_back(n); + nodes.push_back(n); } - + if (label == 0) { - IdMap id_map(_graph); - _writer_bits::MapLess > id_less(id_map); - std::sort(nodes.begin(), nodes.end(), id_less); + IdMap id_map(_graph); + _writer_bits::MapLess > id_less(id_map); + std::sort(nodes.begin(), nodes.end(), id_less); } else { - label->sort(nodes); + label->sort(nodes); } for (int i = 0; i < static_cast(nodes.size()); ++i) { - Node n = nodes[i]; - if (label == 0) { - std::ostringstream os; - os << _graph.id(n); - _writer_bits::writeToken(*_os, os.str()); - *_os << '\t'; - _node_index.insert(std::make_pair(n, os.str())); - } - for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { - std::string value = it->second->get(n); - _writer_bits::writeToken(*_os, value); - if (it->first == "label") { - _node_index.insert(std::make_pair(n, value)); - } - *_os << '\t'; - } - *_os << std::endl; + Node n = nodes[i]; + if (label == 0) { + std::ostringstream os; + os << _graph.id(n); + _writer_bits::writeToken(*_os, os.str()); + *_os << '\t'; + _node_index.insert(std::make_pair(n, os.str())); + } + for (typename NodeMaps::iterator it = _node_maps.begin(); + it != _node_maps.end(); ++it) { + std::string value = it->second->get(n); + _writer_bits::writeToken(*_os, value); + if (it->first == "label") { + _node_index.insert(std::make_pair(n, value)); + } + *_os << '\t'; + } + *_os << std::endl; } } void createNodeIndex() { _writer_bits::MapStorageBase* label = 0; for (typename NodeMaps::iterator it = _node_maps.begin(); - it != _node_maps.end(); ++it) { + it != _node_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } if (label == 0) { - for (NodeIt n(_graph); n != INVALID; ++n) { - std::ostringstream os; - os << _graph.id(n); - _node_index.insert(std::make_pair(n, os.str())); - } + for (NodeIt n(_graph); n != INVALID; ++n) { + std::ostringstream os; + os << _graph.id(n); + _node_index.insert(std::make_pair(n, os.str())); + } } else { - for (NodeIt n(_graph); n != INVALID; ++n) { - std::string value = label->get(n); - _node_index.insert(std::make_pair(n, value)); - } + for (NodeIt n(_graph); n != INVALID; ++n) { + std::string value = label->get(n); + _node_index.insert(std::make_pair(n, value)); + } } } void writeEdges() { _writer_bits::MapStorageBase* label = 0; for (typename EdgeMaps::iterator it = _edge_maps.begin(); - it != _edge_maps.end(); ++it) { + it != _edge_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } *_os << "@edges"; if (!_edges_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _edges_caption); + _writer_bits::writeToken(*_os << ' ', _edges_caption); } *_os << std::endl; *_os << '\t' << '\t'; if (label == 0) { - *_os << "label" << '\t'; + *_os << "label" << '\t'; } for (typename EdgeMaps::iterator it = _edge_maps.begin(); - it != _edge_maps.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << '\t'; + it != _edge_maps.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << '\t'; } *_os << std::endl; std::vector edges; for (EdgeIt n(_graph); n != INVALID; ++n) { - edges.push_back(n); + edges.push_back(n); } - + if (label == 0) { - IdMap id_map(_graph); - _writer_bits::MapLess > id_less(id_map); - std::sort(edges.begin(), edges.end(), id_less); + IdMap id_map(_graph); + _writer_bits::MapLess > id_less(id_map); + std::sort(edges.begin(), edges.end(), id_less); } else { - label->sort(edges); + label->sort(edges); } for (int i = 0; i < static_cast(edges.size()); ++i) { - Edge e = edges[i]; - _writer_bits::writeToken(*_os, _node_index. - find(_graph.u(e))->second); - *_os << '\t'; - _writer_bits::writeToken(*_os, _node_index. - find(_graph.v(e))->second); - *_os << '\t'; - if (label == 0) { - std::ostringstream os; - os << _graph.id(e); - _writer_bits::writeToken(*_os, os.str()); - *_os << '\t'; - _edge_index.insert(std::make_pair(e, os.str())); - } - for (typename EdgeMaps::iterator it = _edge_maps.begin(); - it != _edge_maps.end(); ++it) { - std::string value = it->second->get(e); - _writer_bits::writeToken(*_os, value); - if (it->first == "label") { - _edge_index.insert(std::make_pair(e, value)); - } - *_os << '\t'; - } - *_os << std::endl; + Edge e = edges[i]; + _writer_bits::writeToken(*_os, _node_index. + find(_graph.u(e))->second); + *_os << '\t'; + _writer_bits::writeToken(*_os, _node_index. + find(_graph.v(e))->second); + *_os << '\t'; + if (label == 0) { + std::ostringstream os; + os << _graph.id(e); + _writer_bits::writeToken(*_os, os.str()); + *_os << '\t'; + _edge_index.insert(std::make_pair(e, os.str())); + } + for (typename EdgeMaps::iterator it = _edge_maps.begin(); + it != _edge_maps.end(); ++it) { + std::string value = it->second->get(e); + _writer_bits::writeToken(*_os, value); + if (it->first == "label") { + _edge_index.insert(std::make_pair(e, value)); + } + *_os << '\t'; + } + *_os << std::endl; } } void createEdgeIndex() { _writer_bits::MapStorageBase* label = 0; for (typename EdgeMaps::iterator it = _edge_maps.begin(); - it != _edge_maps.end(); ++it) { + it != _edge_maps.end(); ++it) { if (it->first == "label") { - label = it->second; - break; - } + label = it->second; + break; + } } if (label == 0) { - for (EdgeIt e(_graph); e != INVALID; ++e) { - std::ostringstream os; - os << _graph.id(e); - _edge_index.insert(std::make_pair(e, os.str())); - } + for (EdgeIt e(_graph); e != INVALID; ++e) { + std::ostringstream os; + os << _graph.id(e); + _edge_index.insert(std::make_pair(e, os.str())); + } } else { - for (EdgeIt e(_graph); e != INVALID; ++e) { - std::string value = label->get(e); - _edge_index.insert(std::make_pair(e, value)); - } + for (EdgeIt e(_graph); e != INVALID; ++e) { + std::string value = label->get(e); + _edge_index.insert(std::make_pair(e, value)); + } } } @@ -1419,20 +1419,20 @@ if (_attributes.empty()) return; *_os << "@attributes"; if (!_attributes_caption.empty()) { - _writer_bits::writeToken(*_os << ' ', _attributes_caption); + _writer_bits::writeToken(*_os << ' ', _attributes_caption); } *_os << std::endl; for (typename Attributes::iterator it = _attributes.begin(); - it != _attributes.end(); ++it) { - _writer_bits::writeToken(*_os, it->first) << ' '; - _writer_bits::writeToken(*_os, it->second->get()); - *_os << std::endl; + it != _attributes.end(); ++it) { + _writer_bits::writeToken(*_os, it->first) << ' '; + _writer_bits::writeToken(*_os, it->second->get()); + *_os << std::endl; } } - + public: - - /// \name Execution of the writer + + /// \name Execution of the writer /// @{ /// \brief Start the batch processing @@ -1440,14 +1440,14 @@ /// This function starts the batch processing. void run() { if (!_skip_nodes) { - writeNodes(); + writeNodes(); } else { - createNodeIndex(); + createNodeIndex(); } - if (!_skip_edges) { - writeEdges(); + if (!_skip_edges) { + writeEdges(); } else { - createEdgeIndex(); + createEdgeIndex(); } writeAttributes(); } @@ -1463,7 +1463,7 @@ }; /// \brief Return a \ref GraphWriter class - /// + /// /// This function just returns a \ref GraphWriter class. /// \relates GraphWriter template @@ -1473,7 +1473,7 @@ } /// \brief Return a \ref GraphWriter class - /// + /// /// This function just returns a \ref GraphWriter class. /// \relates GraphWriter template @@ -1483,7 +1483,7 @@ } /// \brief Return a \ref GraphWriter class - /// + /// /// This function just returns a \ref GraphWriter class. /// \relates GraphWriter template