A graph reader interface proposal
authoralpar
Tue, 09 Nov 2004 09:12:35 +0000
changeset 972c0fdb1ad8e8d
parent 971 643d3192ebc8
child 973 6a6f3ac07b20
A graph reader interface proposal
src/work/alpar/file-reader.cc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/work/alpar/file-reader.cc	Tue Nov 09 09:12:35 2004 +0000
     1.3 @@ -0,0 +1,122 @@
     1.4 +#include <lemon/list_graph.h>
     1.5 +#include <lemon/maps.h>
     1.6 +#include <list>
     1.7 +
     1.8 +using namespace lemon;
     1.9 +
    1.10 +template<class GR>
    1.11 +class Reader 
    1.12 +{
    1.13 +  
    1.14 +public:
    1.15 +  typedef GR Graph;
    1.16 +  typedef typename Graph::Node Node;
    1.17 +  typedef typename Graph::Edge Edge;
    1.18 +    
    1.19 +  ///A beolvasando ertekek tipusa.
    1.20 +
    1.21 +  ///Ebben gyakorlatilag egy stringet tarolunk.
    1.22 +  ///Tovabba ez egy olyan tipus, amitol elvarjuk, hogy konvertalodjon
    1.23 +  ///a beolvasando mapok Value-jara.
    1.24 +  class InputValue 
    1.25 +  {
    1.26 +  public:
    1.27 +    operator int()    const { return 0; }
    1.28 +    operator double() const { return 0; }
    1.29 +  };
    1.30 +
    1.31 +  ///Ez adja a mapok kozos interface-et.
    1.32 +  class WrapEdgeMapBase 
    1.33 +  {
    1.34 +  public:
    1.35 +    virtual void set(Edge n,const InputValue &v) = 0;
    1.36 +    virtual ~WrapEdgeMapBase() {}
    1.37 +  };
    1.38 +
    1.39 +  ///Ez meg becsomagol egy mapot a kozos formara.
    1.40 +  template<class M>
    1.41 +  class WrapEdgeMap : public WrapEdgeMapBase
    1.42 +  {
    1.43 +    M &map;
    1.44 +    
    1.45 +  public:
    1.46 +    WrapEdgeMap(M &m) : map(m) {}
    1.47 +    void set(Edge n,const InputValue &v) {map.set(n,v);}
    1.48 +  };
    1.49 +
    1.50 +  ///Ebben a strukturaban adunk meg egy feltoltendo mapot.
    1.51 +  struct EdgeMapDesc 
    1.52 +  {
    1.53 +    std::string name;
    1.54 +    int column;
    1.55 +    WrapEdgeMapBase *map;
    1.56 +    
    1.57 +    template<class M> EdgeMapDesc(char *n,M &m) : name(n), map(&m) {}
    1.58 +    ~EdgeMapDesc() {}    
    1.59 +  };
    1.60 +  
    1.61 +  ///Itt vannak felsorolva a feltoltendo mapok
    1.62 +  std::list<EdgeMapDesc> EdgeMaps;
    1.63 +
    1.64 +  ///Ezzel lehet egy mapot hozzaadni a feltoltendok listajahoz.
    1.65 +  template<class M> void addEdgeMap(char *name,M &map)
    1.66 +  {
    1.67 +    EdgeMaps.push_back(EdgeMapDesc(name,*(new WrapEdgeMap<M>(map))));
    1.68 +  }
    1.69 +  
    1.70 +  ///Ez meg kiuriti a map-listat.
    1.71 +  void removeEdgeMaps() 
    1.72 +  {
    1.73 +    while(EdgeMaps.begin()!=EdgeMaps.end()) {
    1.74 +      //Ez igy OK?:
    1.75 +      delete EdgeMaps.begin()->map;
    1.76 +      EdgeMaps.pop_front();
    1.77 +    }
    1.78 +  }
    1.79 +  
    1.80 +  //Esetleg stream a file-nev helyett...
    1.81 +  Reader(char *file,Graph &G)
    1.82 +  {
    1.83 +     removeEdgeMaps();
    1.84 +  }
    1.85 +  
    1.86 +  ///Ez inditja a beolvasast.
    1.87 +  void read() 
    1.88 +  {
    1.89 +    //Ez olvassa be/vegig a file-t.
    1.90 +  }
    1.91 +  
    1.92 +  ~Reader() 
    1.93 +  {
    1.94 +    //Ez kell!!!
    1.95 +    removeEdgeMaps();
    1.96 +  }
    1.97 +  
    1.98 +};
    1.99 +
   1.100 +class SumMap : public MapBase<ListGraph::Edge,double>
   1.101 +{
   1.102 +  double sum;
   1.103 +public:
   1.104 +  SumMap() : sum(0) {}
   1.105 +  void set(ListGraph::Edge e,double d) 
   1.106 +  {
   1.107 +    sum+=d;
   1.108 +  }
   1.109 +  double getSum() { return sum; }
   1.110 +};
   1.111 +
   1.112 +int main() 
   1.113 +{
   1.114 +  ListGraph g; //Kis g!!!
   1.115 +  ListGraph::EdgeMap<double> cost(g);
   1.116 +  ListGraph::EdgeMap<int> channels(g);
   1.117 +  
   1.118 +  SumMap sum_cost; //Ez egy sajat "custom" irhato map
   1.119 +  
   1.120 +  Reader<ListGraph> r("file_name.lgf",g);
   1.121 +  r.addEdgeMap("cost",cost);
   1.122 +  r.addEdgeMap("channels",channels);
   1.123 +  r.addEdgeMap("cost",sum_cost);
   1.124 +  r.read();
   1.125 +}