src/work/alpar/file-reader.cc
changeset 1308 0274efa2222f
equal deleted inserted replaced
-1:000000000000 0:cb8a4c2f5d23
       
     1 #include <lemon/list_graph.h>
       
     2 #include <lemon/maps.h>
       
     3 #include <list>
       
     4 
       
     5 using namespace lemon;
       
     6 
       
     7 template<class GR>
       
     8 class Reader 
       
     9 {
       
    10   
       
    11 public:
       
    12   typedef GR Graph;
       
    13   typedef typename Graph::Node Node;
       
    14   typedef typename Graph::Edge Edge;
       
    15     
       
    16   ///A beolvasando ertekek tipusa.
       
    17 
       
    18   ///Ebben gyakorlatilag egy stringet tarolunk.
       
    19   ///Tovabba ez egy olyan tipus, amitol elvarjuk, hogy konvertalodjon
       
    20   ///a beolvasando mapok Value-jara.
       
    21   class InputValue 
       
    22   {
       
    23   public:
       
    24     operator int()    const { return 0; }
       
    25     operator double() const { return 0; }
       
    26   };
       
    27 
       
    28   ///Ez adja a mapok kozos interface-et.
       
    29   class WrapEdgeMapBase 
       
    30   {
       
    31   public:
       
    32     virtual void set(Edge n,const InputValue &v) = 0;
       
    33     virtual ~WrapEdgeMapBase() {}
       
    34   };
       
    35 
       
    36   ///Ez meg becsomagol egy mapot a kozos formara.
       
    37   template<class M>
       
    38   class WrapEdgeMap : public WrapEdgeMapBase
       
    39   {
       
    40     M &map;
       
    41     
       
    42   public:
       
    43     WrapEdgeMap(M &m) : map(m) {}
       
    44     void set(Edge n,const InputValue &v) {map.set(n,v);}
       
    45   };
       
    46 
       
    47   ///Ebben a strukturaban adunk meg egy feltoltendo mapot.
       
    48   struct EdgeMapDesc 
       
    49   {
       
    50     std::string name;
       
    51     int column;
       
    52     WrapEdgeMapBase *map;
       
    53     
       
    54     template<class M> EdgeMapDesc(char *n,M &m) : name(n), map(&m) {}
       
    55     ~EdgeMapDesc() {}    
       
    56   };
       
    57   
       
    58   ///Itt vannak felsorolva a feltoltendo mapok
       
    59   std::list<EdgeMapDesc> EdgeMaps;
       
    60 
       
    61   ///Ezzel lehet egy mapot hozzaadni a feltoltendok listajahoz.
       
    62   template<class M> void addEdgeMap(char *name,M &map)
       
    63   {
       
    64     EdgeMaps.push_back(EdgeMapDesc(name,*(new WrapEdgeMap<M>(map))));
       
    65   }
       
    66   
       
    67   ///Ez meg kiuriti a map-listat.
       
    68   void removeEdgeMaps() 
       
    69   {
       
    70     while(EdgeMaps.begin()!=EdgeMaps.end()) {
       
    71       //Ez igy OK?:
       
    72       delete EdgeMaps.begin()->map;
       
    73       EdgeMaps.pop_front();
       
    74     }
       
    75   }
       
    76   
       
    77   //Esetleg stream a file-nev helyett...
       
    78   Reader(char *file,Graph &G)
       
    79   {
       
    80      removeEdgeMaps();
       
    81   }
       
    82   
       
    83   ///Ez inditja a beolvasast.
       
    84   void read() 
       
    85   {
       
    86     //Ez olvassa be/vegig a file-t.
       
    87   }
       
    88   
       
    89   ~Reader() 
       
    90   {
       
    91     //Ez kell!!!
       
    92     removeEdgeMaps();
       
    93   }
       
    94   
       
    95 };
       
    96 
       
    97 class SumMap : public MapBase<ListGraph::Edge,double>
       
    98 {
       
    99   double sum;
       
   100 public:
       
   101   SumMap() : sum(0) {}
       
   102   void set(ListGraph::Edge e,double d) 
       
   103   {
       
   104     sum+=d;
       
   105   }
       
   106   double getSum() { return sum; }
       
   107 };
       
   108 
       
   109 int main() 
       
   110 {
       
   111   ListGraph g; //Kis g!!!
       
   112   ListGraph::EdgeMap<double> cost(g);
       
   113   ListGraph::EdgeMap<int> channels(g);
       
   114   
       
   115   SumMap sum_cost; //Ez egy sajat "custom" irhato map
       
   116   
       
   117   Reader<ListGraph> r("file_name.lgf",g);
       
   118   r.addEdgeMap("cost",cost);
       
   119   r.addEdgeMap("channels",channels);
       
   120   r.addEdgeMap("cost",sum_cost);
       
   121   r.read();
       
   122 }