[Lemon-commits] [lemon_svn] deba: r1627 - hugo/trunk/src/lemon
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:46:40 CET 2006
Author: deba
Date: Wed Mar 9 15:15:22 2005
New Revision: 1627
Modified:
hugo/trunk/src/lemon/graph_reader.h
hugo/trunk/src/lemon/graph_writer.h
Log:
Easy input-output function for common graphs.
Modified Exception handling in graph_reader.
Modified: hugo/trunk/src/lemon/graph_reader.h
==============================================================================
--- hugo/trunk/src/lemon/graph_reader.h (original)
+++ hugo/trunk/src/lemon/graph_reader.h Wed Mar 9 15:15:22 2005
@@ -35,43 +35,6 @@
// Exceptions
- class IOException {
- public:
- virtual ~IOException() {}
- virtual string what() const = 0;
- };
-
- class DataFormatException : public IOException {
- std::string message;
- public:
- DataFormatException(const std::string& _message)
- : message(_message) {}
- std::string what() const {
- return "DataFormatException: " + message;
- }
- };
-
- template <typename _Exception>
- class StreamException : public _Exception {
- public:
- typedef _Exception Exception;
- StreamException(int _line, Exception _exception)
- : Exception(_exception), line_num(_line) {}
- virtual int line() const {
- return line_num;
- }
-
- virtual ~StreamException() {}
-
- virtual std::string what() const {
- ostringstream os;
- os << Exception::what() << " in line " << line();
- return os.str();
- }
- private:
- int line_num;
- };
-
/// \brief Standard ReaderTraits for the GraphReader class.
///
@@ -91,7 +54,7 @@
/// Reads a value from the given stream.
void read(std::istream& is, Value& value) {
if (!(is >> value))
- throw DataFormatException("Default Reader format exception");
+ throw DataFormatError("Default reader format exception");
}
};
@@ -122,7 +85,7 @@
value.clear();
is >> ws;
if (!is.get(c) || c != '\"')
- throw DataFormatException("Quoted string format");
+ throw DataFormatError("Quoted string format error");
while (is.get(c) && c != '\"') {
if (escaped && c == '\\') {
value += readEscape(is);
@@ -130,7 +93,7 @@
value += c;
}
}
- if (!is) throw DataFormatException("Quoted string format");
+ if (!is) throw DataFormatError("Quoted string format error");
}
private:
@@ -164,7 +127,7 @@
{
int code;
if (!is.get(c) || !isHex(c))
- throw DataFormatException("Escape format exception");
+ throw DataFormatError("Escape format error");
else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c);
else code = code * 16 + valueHex(c);
return code;
@@ -173,7 +136,7 @@
{
int code;
if (!isOct(c))
- throw DataFormatException("Escape format exception");
+ throw DataFormatError("Escape format error");
else if (code = valueOct(c), !is.get(c) || !isOct(c))
is.putback(c);
else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c))
@@ -225,9 +188,8 @@
/// \brief Construct a new GraphReader.
///
- /// Construct a new GraphReader. It reads from the given map,
- /// it constructs the given map and it use the given reader as the
- /// default skipper.
+ /// Construct a new GraphReader. It reads into the given graph
+ /// and it use the given reader as the default skipper.
GraphReader(std::istream& _is, Graph& _graph,
const DefaultReader& _reader = DefaultReader())
: is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {}
@@ -265,7 +227,9 @@
GraphReader& addNodeMap(std::string name, Map& map,
const Reader& reader = Reader()) {
if (node_map_readers.find(name) != node_map_readers.end()) {
- throw Exception() << "Multiple read rule for node map: " << name;
+ ErrorMessage msg;
+ msg << "Multiple read rule for node map: " << name;
+ throw IOLogicError(msg.message());
}
node_map_readers.insert(
make_pair(name, new MapReader<Node, Map, Reader>(map, reader)));
@@ -279,7 +243,9 @@
GraphReader& skipNodeMap(std::string name,
const Reader& reader = Reader()) {
if (node_map_readers.find(name) != node_map_readers.end()) {
- throw Exception() << "Multiple read rule for node map: " << name;
+ ErrorMessage msg;
+ msg << "Multiple read rule for node map: " << name;
+ throw IOLogicError(msg.message());
}
node_map_readers.insert(
make_pair(name, new SkipReader<Node, Reader>(reader)));
@@ -303,7 +269,9 @@
GraphReader& addEdgeMap(std::string name, Map& map,
const Reader& reader = Reader()) {
if (edge_map_readers.find(name) != edge_map_readers.end()) {
- throw Exception() << "Multiple read rule for edge map: " << name;
+ ErrorMessage msg;
+ msg << "Multiple read rule for edge map: " << name;
+ throw IOLogicError(msg.message());
}
edge_map_readers.insert(
make_pair(name, new MapReader<Edge, Map, Reader>(map, reader)));
@@ -317,7 +285,9 @@
GraphReader& skipEdgeMap(std::string name,
const Reader& reader = Reader()) {
if (edge_map_readers.find(name) != edge_map_readers.end()) {
- throw Exception() << "Multiple read rule for edge map: " << name;
+ ErrorMessage msg;
+ msg << "Multiple read rule for edge map: " << name;
+ throw IOLogicError(msg.message());
}
edge_map_readers.insert(
make_pair(name, new SkipReader<Edge, Reader>(reader)));
@@ -329,7 +299,9 @@
/// Add a new labeled node reader for the reader.
GraphReader& addNode(std::string name, Node& node) {
if (node_readers.find(name) != node_readers.end()) {
- throw Exception() << "Multiple read rule for node";
+ ErrorMessage msg;
+ msg << "Multiple read rule for node: " << name;
+ throw IOLogicError(msg.message());
}
node_readers.insert(make_pair(name, &node));
return *this;
@@ -340,7 +312,9 @@
/// Add a new labeled edge reader for the reader.
GraphReader& addEdge(std::string name, Edge& edge) {
if (edge_readers.find(name) != edge_readers.end()) {
- throw Exception() << "Multiple read rule for edge";
+ ErrorMessage msg;
+ msg << "Multiple read rule for edge: " << name;
+ throw IOLogicError(msg.message());
}
edge_readers.insert(make_pair(name, &edge));
return *this;
@@ -368,10 +342,11 @@
line = readEdges(line_num, edgeInverter);
}
if (line.find("@end") != 0) {
- throw DataFormatException("Invalid control sequence: " + line);
+ throw DataFormatError("Invalid control sequence error");
}
- } catch (DataFormatException e) {
- throw StreamException<DataFormatException>(line_num, e);
+ } catch (DataFormatError e) {
+ e.line(line_num);
+ throw e;
}
}
@@ -399,7 +374,7 @@
}
if (index.size() == 0) {
- throw DataFormatException("No node map found");
+ throw DataFormatError("Cannot find node id map");
}
nodeInverter = auto_ptr<InverterBase<Node> >(index[0]->getInverter());
@@ -436,7 +411,7 @@
}
if (index.size() == 0) {
- throw DataFormatException("No edge map found");
+ throw DataFormatError("Cannot find edge id map");
}
edgeInverter = auto_ptr<InverterBase<Edge> >(index[0]->getInverter());
@@ -492,7 +467,7 @@
return line.substr(vi);
}
}
- throw DataFormatException("End of stream");
+ throw DataFormatError("End of stream error");
}
// Inverters store and give back the Item from the id,
@@ -532,7 +507,7 @@
if (it == inverse.end()) {
inverse.insert(make_pair(value, item));
} else {
- throw DataFormatException("Multiple ID occurence");
+ throw DataFormatError("Multiple ID occurence");
}
}
@@ -543,7 +518,7 @@
if (it != inverse.end()) {
return it->second;
} else {
- throw DataFormatException("Invalid ID");
+ throw DataFormatError("Invalid ID error");
}
}
};
@@ -570,7 +545,7 @@
if (it == inverse.end()) {
inverse.insert(make_pair(value, item));
} else {
- throw DataFormatException("Multiple ID occurence");
+ throw DataFormatError("Multiple ID occurence error");
}
}
@@ -581,7 +556,7 @@
if (it != inverse.end()) {
return it->second;
} else {
- throw DataFormatException("Invalid ID");
+ throw DataFormatError("Invalid ID error");
}
}
private:
@@ -672,4 +647,49 @@
};
+ /// Ready to use reader function.
+ template<typename Graph, typename CapacityMap, typename CostMap>
+ void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
+ typename Graph::Node &s, typename Graph::Node &t,
+ CostMap& cost) {
+ GraphReader<Graph> reader(is, g);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addEdgeMap("cost", cost);
+ reader.addNode("source", s);
+ reader.addNode("target", t);
+ reader.run();
+ }
+
+ template<typename Graph, typename CapacityMap>
+ void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
+ typename Graph::Node &s, typename Graph::Node &t) {
+ GraphReader<Graph> reader(is, g);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addNode("source", s);
+ reader.addNode("target", t);
+ reader.run();
+ }
+
+ template<typename Graph, typename CapacityMap>
+ void readGraph(std::istream& is, Graph &g, CapacityMap& capacity,
+ typename Graph::Node &s) {
+ GraphReader<Graph> reader(is, g);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addNode("source", s);
+ reader.run();
+ }
+
+ template<typename Graph, typename CapacityMap>
+ void readGraph(std::istream& is, Graph &g, CapacityMap& capacity) {
+ GraphReader<Graph> reader(is, g);
+ reader.addEdgeMap("capacity", capacity);
+ reader.run();
+ }
+
+ template<typename Graph>
+ void readGraph(std::istream& is, Graph &g) {
+ GraphReader<Graph> reader(is, g);
+ reader.run();
+ }
+
}
Modified: hugo/trunk/src/lemon/graph_writer.h
==============================================================================
--- hugo/trunk/src/lemon/graph_writer.h (original)
+++ hugo/trunk/src/lemon/graph_writer.h Wed Mar 9 15:15:22 2005
@@ -27,6 +27,7 @@
#include <memory>
+#include <lemon/map_utils.h>
#include <lemon/invalid.h>
#include <lemon/error.h>
@@ -162,7 +163,8 @@
/// Construct a new GraphWriter. It writes from the given map,
/// it constructs the given map and it use the given writer as the
/// default skipper.
- GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {}
+ GraphWriter(std::ostream& _os, const Graph& _graph)
+ : os(_os), graph(_graph) {}
/// \brief Destruct the graph writer.
@@ -367,9 +369,78 @@
EdgeWriters edge_writers;
std::ostream& os;
- Graph& graph;
+ const Graph& graph;
};
+ /// Ready to use writer function.
+ template<typename Graph, typename CapacityMap, typename CostMap>
+ void writeGraph(std::ostream& os, const Graph &g,
+ const CapacityMap& capacity, const typename Graph::Node &s,
+ const typename Graph::Node &t, const CostMap& cost) {
+ GraphWriter<Graph> reader(os, g);
+ IdMap<Graph, typename Graph::Node> nodeIdMap(g);
+ reader.addNodeMap("id", nodeIdMap);
+ IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
+ reader.addEdgeMap("id", edgeIdMap);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addEdgeMap("cost", cost);
+ reader.addNode("source", s);
+ reader.addNode("target", t);
+ reader.run();
+ }
+
+ /// Ready to use writer function.
+ template<typename Graph, typename CapacityMap, typename CostMap>
+ void writeGraph(std::ostream& os, const Graph &g,
+ const CapacityMap& capacity, const typename Graph::Node &s,
+ const typename Graph::Node &t) {
+ GraphWriter<Graph> reader(os, g);
+ IdMap<Graph, typename Graph::Node> nodeIdMap(g);
+ reader.addNodeMap("id", nodeIdMap);
+ IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
+ reader.addEdgeMap("id", edgeIdMap);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addNode("source", s);
+ reader.addNode("target", t);
+ reader.run();
+ }
+
+ /// Ready to use writer function.
+ template<typename Graph, typename CapacityMap>
+ void writeGraph(std::ostream& os, const Graph &g,
+ const CapacityMap& capacity, const typename Graph::Node &s) {
+ GraphWriter<Graph> reader(os, g);
+ IdMap<Graph, typename Graph::Node> nodeIdMap(g);
+ reader.addNodeMap("id", nodeIdMap);
+ IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
+ reader.addEdgeMap("id", edgeIdMap);
+ reader.addEdgeMap("capacity", capacity);
+ reader.addNode("source", s);
+ reader.run();
+ }
+ /// Ready to use writer function.
+ template<typename Graph, typename CapacityMap>
+ void writeGraph(std::ostream& os, const Graph &g,
+ const CapacityMap& capacity) {
+ GraphWriter<Graph> reader(os, g);
+ IdMap<Graph, typename Graph::Node> nodeIdMap(g);
+ reader.addNodeMap("id", nodeIdMap);
+ IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
+ reader.addEdgeMap("id", edgeIdMap);
+ reader.addEdgeMap("capacity", capacity);
+ reader.run();
+ }
+ /// Ready to use writer function.
+ template<typename Graph>
+ void writeGraph(std::ostream& os, const Graph &g) {
+ GraphWriter<Graph> reader(os, g);
+ IdMap<Graph, typename Graph::Node> nodeIdMap(g);
+ reader.addNodeMap("id", nodeIdMap);
+ IdMap<Graph, typename Graph::Edge> edgeIdMap(g);
+ reader.addEdgeMap("id", edgeIdMap);
+ reader.run();
+ }
+
}
More information about the Lemon-commits
mailing list