src/work/deba/graph_io_test.cc
author alpar
Fri, 25 Mar 2005 11:03:49 +0000
changeset 1258 88dff8bb4bf2
parent 1115 444f69240539
permissions -rw-r--r--
- setRow() added
- more docs
deba@1032
     1
#include <lemon/smart_graph.h>
deba@1037
     2
deba@1210
     3
#include <lemon/map_utils.h>
deba@1037
     4
deba@1210
     5
#include <lemon/graph_reader.h>
deba@1210
     6
#include <lemon/graph_writer.h>
deba@1032
     7
deba@1032
     8
#include <iostream>
deba@1032
     9
#include <fstream>
deba@1032
    10
deba@1032
    11
using namespace std;
deba@1032
    12
using namespace lemon;
deba@1032
    13
deba@1032
    14
int main() {
deba@1032
    15
  ifstream input("test.lgf");
deba@1032
    16
  SmartGraph graph;
deba@1032
    17
  GraphReader<SmartGraph> reader(input, graph);
deba@1037
    18
deba@1036
    19
  SmartGraph::NodeMap<int> id(graph);
deba@1115
    20
  reader.addNodeMap("id", id);
deba@1037
    21
deba@1032
    22
  SmartGraph::NodeMap<int> cost(graph);
deba@1115
    23
  reader.addNodeMap("cost", cost);
deba@1037
    24
 
deba@1032
    25
  SmartGraph::NodeMap<string> color(graph);
deba@1115
    26
  reader.addNodeMap("color", color);
deba@1037
    27
deba@1036
    28
  SmartGraph::NodeMap<string> description(graph);
deba@1115
    29
  reader.addNodeMap<QuotedStringReader>("description", description);
deba@1037
    30
deba@1036
    31
  SmartGraph::EdgeMap<char> mmap(graph);
deba@1115
    32
  reader.addEdgeMap("mmap", mmap);
deba@1037
    33
deba@1036
    34
  reader.skipEdgeMap<QuotedStringReader>("description");
deba@1037
    35
deba@1037
    36
  SmartGraph::Node source;
deba@1115
    37
  reader.addNode("source", source);
deba@1037
    38
  
deba@1037
    39
  SmartGraph::Edge newedge;
deba@1115
    40
  reader.addEdge("newedge", newedge);
deba@1037
    41
deba@1036
    42
  try {
deba@1210
    43
    reader.run();
deba@1210
    44
  } catch (IOError& e) {
deba@1036
    45
    cerr << e.what() << endl;
deba@1036
    46
  } catch (Exception e) {
deba@1036
    47
    cerr << e.what() << endl;
deba@1036
    48
  }
deba@1115
    49
deba@1032
    50
  for (SmartGraph::NodeIt it(graph); it != INVALID; ++it) {
deba@1036
    51
    cout << cost[it] << ' ' << color[it] << ' ' << description[it] << endl;
deba@1036
    52
  }
deba@1036
    53
deba@1036
    54
  for (SmartGraph::EdgeIt it(graph); it != INVALID; ++it) {
deba@1115
    55
    cout << mmap[it] << ' ' << id[graph.source(it)] << ' ' << 
deba@1115
    56
      id[graph.target(it)]  << endl;
deba@1032
    57
  }
deba@1037
    58
deba@1115
    59
  cout << id[source] << ' ' << cost[source] << ' ' <<
deba@1115
    60
    color[source] << ' ' << description[source] << endl;
deba@1115
    61
  cout << mmap[newedge] << ' ' << id[graph.source(newedge)] << 
deba@1115
    62
    ' ' << id[graph.target(newedge)]  << endl;
deba@1037
    63
deba@1037
    64
  ofstream output("copy.lgf");
deba@1037
    65
  GraphWriter<SmartGraph> writer(output, graph);
deba@1037
    66
  
deba@1115
    67
  DescriptorMap<SmartGraph, SmartGraph::Node, SmartGraph::NodeMap<int> > 
deba@1115
    68
    node_ids(graph);
deba@1037
    69
  
deba@1115
    70
  writer.addNodeMap("id", node_ids);
deba@1115
    71
  writer.addNodeMap<QuotedStringWriter>("format", description);
deba@1037
    72
deba@1115
    73
  IdMap<SmartGraph, SmartGraph::Edge > edge_ids(graph);
deba@1037
    74
deba@1115
    75
  writer.addEdgeMap("id", edge_ids);
deba@1115
    76
  writer.addEdgeMap("chars", mmap);
deba@1037
    77
  
deba@1115
    78
  writer.addNode("source", node_ids.inverse()[3]);
deba@1115
    79
  //  writer.addEdge("elek", edge_ids.inverse()[6]);
deba@1210
    80
  writer.run();
deba@1037
    81
  
deba@1032
    82
  return 0;
deba@1032
    83
}