vmap.h: Enables one to create maps with a virtual base class.
authoralpar
Wed, 22 Feb 2006 12:45:59 +0000
changeset 1978ef2d00e46897
parent 1977 8ef02f0c4245
child 1979 c2992fd74dad
vmap.h: Enables one to create maps with a virtual base class.
lemon/vmap.h
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/vmap.h	Wed Feb 22 12:45:59 2006 +0000
     1.3 @@ -0,0 +1,112 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2006
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_LIST_GRAPH_H
    1.23 +#define LEMON_LIST_GRAPH_H
    1.24 +
    1.25 +///\ingroup graphs
    1.26 +///\file
    1.27 +///\brief Virtual map support.
    1.28 +
    1.29 +namespace lemon {
    1.30 +
    1.31 +  ///Base class of the virtual maps.
    1.32 +  template<class K, class V>
    1.33 +  class VMapBase 
    1.34 +  {
    1.35 +  public:
    1.36 +    ///\e
    1.37 +    typedef K Key;
    1.38 +    ///\e
    1.39 +    typedef V Value;
    1.40 +    
    1.41 +    ///\e
    1.42 +    virtual Value operator[](const Key &) const { return Value();}
    1.43 +    ///\e
    1.44 +    virtual void set(const Key &,const Value &) {}
    1.45 +  };
    1.46 +  
    1.47 +  ///Base class of the virtual reference maps.
    1.48 +  template<class K, class V>
    1.49 +  class VRefMapBase 
    1.50 +  {
    1.51 +  public:
    1.52 +    ///\e
    1.53 +    typedef K Key;
    1.54 +    ///\e
    1.55 +    typedef V Value;
    1.56 +    
    1.57 +    ///\e
    1.58 +    virtual Value& operator[](Key &) = 0;
    1.59 +    ///\e
    1.60 +    virtual Value& operator[](const Key &) const = 0;
    1.61 +    ///\e
    1.62 +    virtual void set(const Key &,const Value &) = 0;
    1.63 +  };
    1.64 +  
    1.65 +  ///Makes a virtual map from a \ref concept::ReadMap "ReadMap"
    1.66 +  template<class M, class K=typename M::Key,class V=typename M::Value>
    1.67 +  class VReadMap : public VMapBase<K,V> 
    1.68 +  {
    1.69 +    const M &map;
    1.70 +  public:
    1.71 +  ///\e
    1.72 +    VReadMap(const M &m) : map(m) {}
    1.73 +    virtual Value operator[](const Key &k) const { return map[k];}
    1.74 +  };
    1.75 +
    1.76 +  ///Function interface for \ref VReadMap.
    1.77 +  template<class M, class K=typename M::Key,class V=typename M::Value>
    1.78 +  vReadMap(const M& m) {return VReadMap<M,K,V>(m);}
    1.79 +
    1.80 +  ///Makes a virtual map from a \ref concept::WriteMap "WriteMap"
    1.81 +  template<class M, class K=typename M::Key,class V=typename M::Value>
    1.82 +  class VWriteMap :public VMapBase<K,V> 
    1.83 +  {
    1.84 +    M &map;
    1.85 +  public:
    1.86 +    ///\e
    1.87 +    VWriteMap(M &m) : map(m) {}
    1.88 +    virtual void set(const Key &k,const Value &v) { map.set(k,v);}
    1.89 +  };
    1.90 +
    1.91 +  ///Function interface for \ref VWriteMap.
    1.92 +  template<class M, class K=typename M::Key,class V=typename M::Value>
    1.93 +  vWriteMap(const M& m) {return VReadMap<M,K,V>(m);}
    1.94 +
    1.95 +  ///Makes a virtual map from a \ref concept::ReadWriteMap "ReadWriteMap"
    1.96 +  template<class M, class K=typename M::Key,class V=typename M::Value>
    1.97 +  class VReadWriteMap :public VMapBase<K,V>
    1.98 +  {
    1.99 +    M &map;
   1.100 +  public:
   1.101 +    ///\e
   1.102 +    VReadWriteMap(M &m) : map(m) {}
   1.103 +    virtual Value operator[](const Key &k) const { return map[k];}
   1.104 +    virtual void set(const Key &k,const Value &v) { map.set(k,v);}
   1.105 +  };
   1.106 +    
   1.107 +  ///Function interface for \ref VReadWriteMap.
   1.108 +  template<class M, class K=typename M::Key,class V=typename M::Value>
   1.109 +  vReadWriteMap(const M& m) {return VReadMap<M,K,V>(m);}
   1.110 +  
   1.111 +  /// @}  
   1.112 +} //namespace lemon
   1.113 +  
   1.114 +
   1.115 +#endif