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