src/work/deba/graph_io_test.cc
changeset 1064 5f0a20861a77
parent 1036 2f514b5c7122
child 1115 444f69240539
equal deleted inserted replaced
1:374ec5cadc32 2:445c7bfec199
     1 #include <lemon/smart_graph.h>
     1 #include <lemon/smart_graph.h>
       
     2 
       
     3 #include "map_utils.h"
       
     4 
       
     5 
     2 #include "graph_reader.h"
     6 #include "graph_reader.h"
       
     7 #include "graph_writer.h"
     3 
     8 
     4 #include <iostream>
     9 #include <iostream>
     5 #include <fstream>
    10 #include <fstream>
     6 
    11 
     7 using namespace std;
    12 using namespace std;
     9 
    14 
    10 int main() {
    15 int main() {
    11   ifstream input("test.lgf");
    16   ifstream input("test.lgf");
    12   SmartGraph graph;
    17   SmartGraph graph;
    13   GraphReader<SmartGraph> reader(input, graph);
    18   GraphReader<SmartGraph> reader(input, graph);
       
    19 
    14   SmartGraph::NodeMap<int> id(graph);
    20   SmartGraph::NodeMap<int> id(graph);
    15   reader.readNodeMap("id", id);
    21   reader.readNodeMap("id", id);
       
    22 
    16   SmartGraph::NodeMap<int> cost(graph);
    23   SmartGraph::NodeMap<int> cost(graph);
    17   reader.readNodeMap("cost", cost);
    24   reader.readNodeMap("cost", cost);
       
    25  
    18   SmartGraph::NodeMap<string> color(graph);
    26   SmartGraph::NodeMap<string> color(graph);
    19   reader.readNodeMap("color", color);
    27   reader.readNodeMap("color", color);
       
    28 
    20   SmartGraph::NodeMap<string> description(graph);
    29   SmartGraph::NodeMap<string> description(graph);
    21   reader.readNodeMap<QuotedStringReader>("description", description);
    30   reader.readNodeMap<QuotedStringReader>("description", description);
       
    31 
    22   SmartGraph::EdgeMap<char> mmap(graph);
    32   SmartGraph::EdgeMap<char> mmap(graph);
    23   reader.readEdgeMap("mmap", mmap);
    33   reader.readEdgeMap("mmap", mmap);
       
    34 
    24   reader.skipEdgeMap<QuotedStringReader>("description");
    35   reader.skipEdgeMap<QuotedStringReader>("description");
       
    36 
       
    37   SmartGraph::Node source;
       
    38   reader.readNode("source", source);
       
    39   
       
    40   SmartGraph::Edge newedge;
       
    41   reader.readEdge("newedge", newedge);
       
    42 
    25   try {
    43   try {
    26     reader.read();
    44     reader.read();
    27   } catch (IOException& e) {
    45   } catch (IOException& e) {
    28     cerr << e.what() << endl;
    46     cerr << e.what() << endl;
    29   } catch (Exception e) {
    47   } catch (Exception e) {
    34   }
    52   }
    35 
    53 
    36   for (SmartGraph::EdgeIt it(graph); it != INVALID; ++it) {
    54   for (SmartGraph::EdgeIt it(graph); it != INVALID; ++it) {
    37     cout << mmap[it] << ' ' << id[graph.source(it)] << ' ' << id[graph.target(it)]  << endl;
    55     cout << mmap[it] << ' ' << id[graph.source(it)] << ' ' << id[graph.target(it)]  << endl;
    38   }
    56   }
       
    57 
       
    58   cout << id[source] << ' ' << cost[source] << ' ' << color[source] << ' ' << description[source] << endl;
       
    59   cout << mmap[newedge] << ' ' << id[graph.source(newedge)] << ' ' << id[graph.target(newedge)]  << endl;
       
    60 
       
    61   ofstream output("copy.lgf");
       
    62   GraphWriter<SmartGraph> writer(output, graph);
       
    63   
       
    64   DescriptorMap<SmartGraph, SmartGraph::Node, SmartGraph::NodeIt, SmartGraph::NodeMap<int> > node_ids(graph);
       
    65   
       
    66   writer.writeNodeMap("id", node_ids);
       
    67   writer.writeNodeMap<QuotedStringWriter>("format", description);
       
    68 
       
    69   DescriptorMap<SmartGraph, SmartGraph::Edge, SmartGraph::EdgeIt, SmartGraph::EdgeMap<int> > edge_ids(graph);
       
    70 
       
    71   writer.writeEdgeMap("id", edge_ids);
       
    72   writer.writeEdgeMap("chars", mmap);
       
    73   
       
    74   writer.writeNode("source", node_ids.inverse()[3]);
       
    75   writer.writeEdge("elek", edge_ids.inverse()[6]);
       
    76   writer.write();
       
    77   
    39   return 0;
    78   return 0;
    40 }
    79 }