[Lemon-commits] Balazs Dezso: Improvments in map name handling
Lemon HG
hg at lemon.cs.elte.hu
Wed Jul 2 12:08:51 CEST 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/9159de5e9657
changeset: 186:9159de5e9657
user: Balazs Dezso <deba [at] inf.elte.hu>
date: Wed Jul 02 10:37:14 2008 +0200
description:
Improvments in map name handling
- The label map is not necessary
- If the item set is empty and no map is read, then the map name line
can be ommited
diffstat:
1 file changed, 78 insertions(+), 34 deletions(-)
lemon/lgf_reader.h | 112 ++++++++++++++++++++++++++++++++++++----------------
diffs (242 lines):
diff -r 33e45a9b868c -r 9159de5e9657 lemon/lgf_reader.h
--- a/lemon/lgf_reader.h Tue Jul 01 21:21:49 2008 +0200
+++ b/lemon/lgf_reader.h Wed Jul 02 10:37:14 2008 +0200
@@ -881,9 +881,15 @@
std::vector<int> map_index(_node_maps.size());
int map_num, label_index;
- if (!readLine())
- throw DataFormatError("Cannot find map captions");
-
+ char c;
+ if (!readLine() || !(line >> c) || c == '@') {
+ if (readSuccess() && line) line.putback(c);
+ if (!_node_maps.empty())
+ throw DataFormatError("Cannot find map names");
+ return;
+ }
+ line.putback(c);
+
{
std::map<std::string, int> maps;
@@ -912,14 +918,15 @@
{
std::map<std::string, int>::iterator jt = maps.find("label");
- if (jt == maps.end())
- throw DataFormatError("Label map not found in file");
- label_index = jt->second;
+ if (jt != maps.end()) {
+ label_index = jt->second;
+ } else {
+ label_index = -1;
+ }
}
map_num = maps.size();
}
- char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
@@ -937,8 +944,11 @@
Node n;
if (!_use_nodes) {
n = _digraph.addNode();
- _node_index.insert(std::make_pair(tokens[label_index], n));
+ if (label_index != -1)
+ _node_index.insert(std::make_pair(tokens[label_index], n));
} else {
+ if (label_index == -1)
+ throw DataFormatError("Label map not found in file");
typename std::map<std::string, Node>::iterator it =
_node_index.find(tokens[label_index]);
if (it == _node_index.end()) {
@@ -964,8 +974,14 @@
std::vector<int> map_index(_arc_maps.size());
int map_num, label_index;
- if (!readLine())
- throw DataFormatError("Cannot find map captions");
+ char c;
+ if (!readLine() || !(line >> c) || c == '@') {
+ if (readSuccess() && line) line.putback(c);
+ if (!_arc_maps.empty())
+ throw DataFormatError("Cannot find map names");
+ return;
+ }
+ line.putback(c);
{
std::map<std::string, int> maps;
@@ -995,14 +1011,15 @@
{
std::map<std::string, int>::iterator jt = maps.find("label");
- if (jt == maps.end())
- throw DataFormatError("Label map not found in file");
- label_index = jt->second;
+ if (jt != maps.end()) {
+ label_index = jt->second;
+ } else {
+ label_index = -1;
+ }
}
map_num = maps.size();
}
- char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
@@ -1013,7 +1030,7 @@
throw DataFormatError("Source not found");
if (!_reader_bits::readToken(line, target_token))
- throw DataFormatError("Source not found");
+ throw DataFormatError("Target not found");
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
@@ -1048,8 +1065,11 @@
Node target = it->second;
a = _digraph.addArc(source, target);
- _arc_index.insert(std::make_pair(tokens[label_index], a));
+ if (label_index != -1)
+ _arc_index.insert(std::make_pair(tokens[label_index], a));
} else {
+ if (label_index == -1)
+ throw DataFormatError("Label map not found in file");
typename std::map<std::string, Arc>::iterator it =
_arc_index.find(tokens[label_index]);
if (it == _arc_index.end()) {
@@ -1723,8 +1743,14 @@
std::vector<int> map_index(_node_maps.size());
int map_num, label_index;
- if (!readLine())
- throw DataFormatError("Cannot find map captions");
+ char c;
+ if (!readLine() || !(line >> c) || c == '@') {
+ if (readSuccess() && line) line.putback(c);
+ if (!_node_maps.empty())
+ throw DataFormatError("Cannot find map names");
+ return;
+ }
+ line.putback(c);
{
std::map<std::string, int> maps;
@@ -1754,14 +1780,15 @@
{
std::map<std::string, int>::iterator jt = maps.find("label");
- if (jt == maps.end())
- throw DataFormatError("Label map not found in file");
- label_index = jt->second;
+ if (jt != maps.end()) {
+ label_index = jt->second;
+ } else {
+ label_index = -1;
+ }
}
map_num = maps.size();
}
- char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
@@ -1779,8 +1806,11 @@
Node n;
if (!_use_nodes) {
n = _graph.addNode();
- _node_index.insert(std::make_pair(tokens[label_index], n));
+ if (label_index != -1)
+ _node_index.insert(std::make_pair(tokens[label_index], n));
} else {
+ if (label_index == -1)
+ throw DataFormatError("Label map not found in file");
typename std::map<std::string, Node>::iterator it =
_node_index.find(tokens[label_index]);
if (it == _node_index.end()) {
@@ -1806,8 +1836,14 @@
std::vector<int> map_index(_edge_maps.size());
int map_num, label_index;
- if (!readLine())
- throw DataFormatError("Cannot find map captions");
+ char c;
+ if (!readLine() || !(line >> c) || c == '@') {
+ if (readSuccess() && line) line.putback(c);
+ if (!_edge_maps.empty())
+ throw DataFormatError("Cannot find map names");
+ return;
+ }
+ line.putback(c);
{
std::map<std::string, int> maps;
@@ -1837,14 +1873,15 @@
{
std::map<std::string, int>::iterator jt = maps.find("label");
- if (jt == maps.end())
- throw DataFormatError("Label map not found in file");
- label_index = jt->second;
+ if (jt != maps.end()) {
+ label_index = jt->second;
+ } else {
+ label_index = -1;
+ }
}
map_num = maps.size();
}
- char c;
while (readLine() && line >> c && c != '@') {
line.putback(c);
@@ -1852,10 +1889,10 @@
std::string target_token;
if (!_reader_bits::readToken(line, source_token))
- throw DataFormatError("Source not found");
+ throw DataFormatError("Node u not found");
if (!_reader_bits::readToken(line, target_token))
- throw DataFormatError("Source not found");
+ throw DataFormatError("Node v not found");
std::vector<std::string> tokens(map_num);
for (int i = 0; i < map_num; ++i) {
@@ -1890,8 +1927,11 @@
Node target = it->second;
e = _graph.addEdge(source, target);
- _edge_index.insert(std::make_pair(tokens[label_index], e));
+ if (label_index != -1)
+ _edge_index.insert(std::make_pair(tokens[label_index], e));
} else {
+ if (label_index == -1)
+ throw DataFormatError("Label map not found in file");
typename std::map<std::string, Edge>::iterator it =
_edge_index.find(tokens[label_index]);
if (it == _edge_index.end()) {
@@ -2325,8 +2365,12 @@
}
void readMaps(std::vector<std::string>& maps) {
- if (!readLine())
- throw DataFormatError("Cannot find map captions");
+ char c;
+ if (!readLine() || !(line >> c) || c == '@') {
+ if (readSuccess() && line) line.putback(c);
+ return;
+ }
+ line.putback(c);
std::string map;
while (_reader_bits::readToken(line, map)) {
maps.push_back(map);
More information about the Lemon-commits
mailing list