src/work/alpar/rw_nonref_map.cc
author alpar
Wed, 05 Jan 2005 12:26:59 +0000
changeset 1052 172ce6c3ac6e
parent 986 e997802b855c
permissions -rw-r--r--
Two examples showing a bit more convenient way to implement named parameters.
alpar@342
     1
#include <iostream>
alpar@342
     2
#include <smart_graph.h>
alpar@342
     3
alpar@921
     4
using namespace lemon;
alpar@342
     5
alpar@342
     6
template<class GG,class TT>
alpar@342
     7
class CinCoutMap
alpar@342
     8
{
alpar@342
     9
public:
alpar@342
    10
  typedef GG Graph;
alpar@342
    11
  typedef typename GG::Edge Edge;
alpar@342
    12
  
alpar@987
    13
  typedef Edge Key;
alpar@987
    14
  typedef TT Value;
alpar@342
    15
  
alpar@342
    16
  class RefType 
alpar@342
    17
  {
alpar@342
    18
    Graph &G;
alpar@342
    19
    Edge e;
alpar@342
    20
  public:
alpar@342
    21
    RefType(Graph &_G,Edge _e) : G(_G), e(_e) { }
alpar@342
    22
    
alpar@987
    23
    operator Value() const 
alpar@342
    24
    {
alpar@987
    25
      Value tmp;
alpar@986
    26
      std::cout << G.id(G.source(e)) << "->"
alpar@986
    27
		<< G.id(G.target(e)) << ": ";
alpar@342
    28
      std::cin  >> tmp;
alpar@342
    29
      return tmp;
alpar@342
    30
    }
alpar@987
    31
    Value operator = (Value v) const
alpar@342
    32
    {
alpar@986
    33
      std::cout << G.id(G.source(e)) << "->"
alpar@986
    34
		<< G.id(G.target(e)) << ": " << v << '\n';
alpar@342
    35
      return v;
alpar@342
    36
    }
alpar@342
    37
  };
alpar@342
    38
  
alpar@342
    39
private:
alpar@342
    40
  Graph &G;
alpar@342
    41
public:
alpar@342
    42
  CinCoutMap(Graph &_G) : G(_G) { }
alpar@342
    43
  RefType operator[] (Edge e) const { return RefType(G,e);}  
alpar@342
    44
};
alpar@342
    45
alpar@351
    46
template<class K,class T>
alpar@351
    47
class NullMap
alpar@351
    48
{
alpar@351
    49
public:
alpar@987
    50
  typedef K Key;
alpar@987
    51
  typedef T Value;
alpar@351
    52
  
alpar@351
    53
  class RefType 
alpar@351
    54
  {
alpar@987
    55
    Value val;
alpar@351
    56
  public:
alpar@987
    57
    RefType(Value v) : val(v) { }   
alpar@987
    58
    operator Value() const { return val; }
alpar@987
    59
    Value operator = (Value v) const { return val; }
alpar@351
    60
  };
alpar@351
    61
  
alpar@351
    62
private:
alpar@987
    63
  Value val;
alpar@351
    64
public:
alpar@987
    65
  NullMap(Value v) : val(v) { }
alpar@987
    66
  RefType operator[] (Key e) const { return RefType(v);}  
alpar@351
    67
};
alpar@351
    68
alpar@342
    69
int main()
alpar@342
    70
{
alpar@342
    71
  typedef SmartGraph Graph;
alpar@342
    72
  typedef Graph::NodeIt NodeIt;
alpar@342
    73
  typedef Graph::OutEdgeIt OutEdgeIt;
alpar@342
    74
  typedef Graph::EdgeIt EdgeIt;
alpar@342
    75
  
alpar@342
    76
  Graph G;
alpar@342
    77
alpar@342
    78
  CinCoutMap<Graph,int> map(G);
alpar@342
    79
alpar@342
    80
  Graph::EdgeMap<int> emap(G);
alpar@342
    81
  
alpar@342
    82
  for(int i=0;i<3;i++) G.addNode();
alpar@342
    83
alpar@342
    84
  for(NodeIt n(G);G.valid(n);G.next(n))
alpar@342
    85
    for(NodeIt m(G);G.valid(m);G.next(m)) if(n!=m)
alpar@342
    86
      G.addEdge(n,m);
alpar@342
    87
alpar@342
    88
  //for(OutEdgeIt e(G,NodeIt(G));G.valid(e);G.next(e))
alpar@342
    89
    
alpar@342
    90
  for(EdgeIt e(G);G.valid(e);G.next(e)) emap[e] = map[e];
alpar@342
    91
  
alpar@342
    92
  std::cout << '\n';
alpar@342
    93
  
alpar@342
    94
  for(EdgeIt e(G);G.valid(e);G.next(e))  map[e] = emap[e];
alpar@342
    95
alpar@342
    96
}
alpar@342
    97