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