Smart reference handling in map adaptors
authordeba
Sat, 14 May 2005 17:37:33 +0000
changeset 1420e37cca875667
parent 1419 c3244a26adb1
child 1421 7a21e1414c38
Smart reference handling in map adaptors
src/lemon/maps.h
src/lemon/xy.h
     1.1 --- a/src/lemon/maps.h	Sat May 14 17:34:31 2005 +0000
     1.2 +++ b/src/lemon/maps.h	Sat May 14 17:37:33 2005 +0000
     1.3 @@ -17,6 +17,9 @@
     1.4  #ifndef LEMON_MAPS_H
     1.5  #define LEMON_MAPS_H
     1.6  
     1.7 +#include <lemon/graph_utils.h>
     1.8 +#include <lemon/utility.h>
     1.9 +
    1.10  
    1.11  ///\file
    1.12  ///\ingroup maps
    1.13 @@ -55,6 +58,8 @@
    1.14    class NullMap : public MapBase<K,T>
    1.15    {
    1.16    public:
    1.17 +    
    1.18 +    typedef True NeedCopy;
    1.19  
    1.20      /// Gives back a default constructed element.
    1.21      T operator[](const K&) const { return T(); }
    1.22 @@ -62,6 +67,11 @@
    1.23      void set(const K&, const T&) {}
    1.24    };
    1.25  
    1.26 +  template <typename K, typename V> 
    1.27 +  NullMap<K, V> nullMap() {
    1.28 +    return NullMap<K, V>();
    1.29 +  }
    1.30 +
    1.31  
    1.32    /// Constant map.
    1.33  
    1.34 @@ -74,6 +84,8 @@
    1.35      T v;
    1.36    public:
    1.37  
    1.38 +    typedef True NeedCopy;
    1.39 +
    1.40      /// Default constructor
    1.41  
    1.42      /// The value of the map will be uninitialized. 
    1.43 @@ -208,10 +220,12 @@
    1.44    ///\endcode
    1.45    ///\bug wrong documentation
    1.46    template<class M, class T> 
    1.47 -  class ConvertMap
    1.48 -  {
    1.49 -    const M &m;
    1.50 +  class ConvertMap {
    1.51 +    typename SmartConstReference<M>::Type m;
    1.52    public:
    1.53 +
    1.54 +    typedef True NeedCopy;
    1.55 +
    1.56      typedef typename M::Key Key;
    1.57      typedef T Value;
    1.58  
    1.59 @@ -250,9 +264,13 @@
    1.60    template<class M1,class M2> 
    1.61    class AddMap
    1.62    {
    1.63 -    const M1 &m1;
    1.64 -    const M2 &m2;
    1.65 +    typename SmartConstReference<M1>::Type m1;
    1.66 +    typename SmartConstReference<M2>::Type m2;
    1.67 +
    1.68    public:
    1.69 +
    1.70 +    typedef True NeedCopy;
    1.71 +
    1.72      typedef typename M1::Key Key;
    1.73      typedef typename M1::Value Value;
    1.74  
    1.75 @@ -295,9 +313,11 @@
    1.76    template<class M> 
    1.77    class ShiftMap
    1.78    {
    1.79 -    const M &m;
    1.80 +    typename SmartConstReference<M>::Type m;
    1.81      typename M::Value v;
    1.82    public:
    1.83 +
    1.84 +    typedef True NeedCopy;
    1.85      typedef typename M::Key Key;
    1.86      typedef typename M::Value Value;
    1.87  
    1.88 @@ -331,9 +351,11 @@
    1.89    template<class M1,class M2> 
    1.90    class SubMap
    1.91    {
    1.92 -    const M1 &m1;
    1.93 -    const M2 &m2;
    1.94 +    typename SmartConstReference<M1>::Type m1;
    1.95 +    typename SmartConstReference<M2>::Type m2;
    1.96    public:
    1.97 +
    1.98 +    typedef True NeedCopy;
    1.99      typedef typename M1::Key Key;
   1.100      typedef typename M1::Value Value;
   1.101  
   1.102 @@ -367,9 +389,11 @@
   1.103    template<class M1,class M2> 
   1.104    class MulMap
   1.105    {
   1.106 -    const M1 &m1;
   1.107 -    const M2 &m2;
   1.108 +    typename SmartConstReference<M1>::Type m1;
   1.109 +    typename SmartConstReference<M2>::Type m2;
   1.110    public:
   1.111 +
   1.112 +    typedef True NeedCopy;
   1.113      typedef typename M1::Key Key;
   1.114      typedef typename M1::Value Value;
   1.115  
   1.116 @@ -409,9 +433,11 @@
   1.117    template<class M> 
   1.118    class ScaleMap
   1.119    {
   1.120 -    const M &m;
   1.121 +    typename SmartConstReference<M>::Type m;
   1.122      typename M::Value v;
   1.123    public:
   1.124 +
   1.125 +    typedef True NeedCopy;
   1.126      typedef typename M::Key Key;
   1.127      typedef typename M::Value Value;
   1.128  
   1.129 @@ -445,9 +471,11 @@
   1.130    template<class M1,class M2> 
   1.131    class DivMap
   1.132    {
   1.133 -    const M1 &m1;
   1.134 -    const M2 &m2;
   1.135 +    typename SmartConstReference<M1>::Type m1;
   1.136 +    typename SmartConstReference<M2>::Type m2;
   1.137    public:
   1.138 +
   1.139 +    typedef True NeedCopy;
   1.140      typedef typename M1::Key Key;
   1.141      typedef typename M1::Value Value;
   1.142  
   1.143 @@ -489,12 +517,16 @@
   1.144    template<class M1,class M2> 
   1.145    class ComposeMap
   1.146    {
   1.147 -    const M1 &m1;
   1.148 -    const M2 &m2;
   1.149 +    typename SmartConstReference<M1>::Type m1;
   1.150 +    typename SmartConstReference<M2>::Type m2;
   1.151    public:
   1.152 +
   1.153 +    typedef True NeedCopy;
   1.154      typedef typename M2::Key Key;
   1.155      typedef typename M1::Value Value;
   1.156  
   1.157 +    typedef True NeedCopy;
   1.158 +
   1.159      ///Constructor
   1.160  
   1.161      ///\e
   1.162 @@ -536,13 +568,15 @@
   1.163    ///to \c V.
   1.164    ///\todo Check the requirements.
   1.165  
   1.166 -  template<class M1,class M2,class F,class V> 
   1.167 +  template<class M1,class M2,class F,class V = typename F::result_type> 
   1.168    class CombineMap
   1.169    {
   1.170 -    const M1 &m1;
   1.171 -    const M2 &m2;
   1.172 -    const F &f;
   1.173 +    typename SmartConstReference<M1>::Type m1;
   1.174 +    typename SmartConstReference<M2>::Type m2;
   1.175 +    F f;
   1.176    public:
   1.177 +
   1.178 +    typedef True NeedCopy;
   1.179      typedef typename M1::Key Key;
   1.180      typedef V Value;
   1.181  
   1.182 @@ -571,10 +605,10 @@
   1.183    ///\endcode
   1.184    ///
   1.185    ///\relates CombineMap
   1.186 -  template<class V,class M1,class M2,class F> 
   1.187 -  inline CombineMap<M1,M2,F,V> combineMap(const M1 &m1,const M2 &m2,const F &f) 
   1.188 +  template<class M1,class M2,class F> 
   1.189 +  inline CombineMap<M1,M2,F> combineMap(const M1 &m1,const M2 &m2,const F &f) 
   1.190    {
   1.191 -    return CombineMap<M1,M2,F,V>(m1,m2,f);
   1.192 +    return CombineMap<M1,M2,F>(m1,m2,f);
   1.193    }
   1.194  
   1.195    ///Negative value of a map
   1.196 @@ -588,8 +622,10 @@
   1.197    template<class M> 
   1.198    class NegMap
   1.199    {
   1.200 -    const M &m;
   1.201 +    typename SmartConstReference<M>::Type m;
   1.202    public:
   1.203 +
   1.204 +    typedef True NeedCopy;
   1.205      typedef typename M::Key Key;
   1.206      typedef typename M::Value Value;
   1.207  
   1.208 @@ -638,8 +674,10 @@
   1.209    template<class M> 
   1.210    class AbsMap
   1.211    {
   1.212 -    const M &m;
   1.213 +    typename SmartConstReference<M>::Type m;
   1.214    public:
   1.215 +
   1.216 +    typedef True NeedCopy;
   1.217      typedef typename M::Key Key;
   1.218      typedef typename M::Value Value;
   1.219  
   1.220 @@ -679,6 +717,8 @@
   1.221    {
   1.222      const F &f;
   1.223    public:
   1.224 +
   1.225 +    typedef True NeedCopy;
   1.226      typedef K Key;
   1.227      typedef V Value;
   1.228  
   1.229 @@ -714,8 +754,10 @@
   1.230    template<class M> 
   1.231    class MapFunctor
   1.232    {
   1.233 -    const M &m;
   1.234 +    typename SmartConstReference<M>::Type m;
   1.235    public:
   1.236 +
   1.237 +    typedef True NeedCopy;
   1.238      typedef typename M::Key argument_type;
   1.239      typedef typename M::Value result_type;
   1.240      typedef typename M::Key Key;
   1.241 @@ -761,9 +803,11 @@
   1.242    template<class M1,class M2> 
   1.243    class ForkMap
   1.244    {
   1.245 -    const M1 &m1;
   1.246 -    const M2 &m2;
   1.247 +    typename SmartConstReference<M1>::Type m1;
   1.248 +    typename SmartConstReference<M2>::Type m2;
   1.249    public:
   1.250 +
   1.251 +    typedef True NeedCopy;
   1.252      typedef typename M1::Key Key;
   1.253      typedef typename M1::Value Value;
   1.254  
     2.1 --- a/src/lemon/xy.h	Sat May 14 17:34:31 2005 +0000
     2.2 +++ b/src/lemon/xy.h	Sat May 14 17:37:33 2005 +0000
     2.3 @@ -18,6 +18,7 @@
     2.4  #define LEMON_XY_H
     2.5  
     2.6  #include <iostream>
     2.7 +#include <lemon/utility.h>
     2.8  
     2.9  ///\ingroup misc
    2.10  ///\file
    2.11 @@ -350,12 +351,14 @@
    2.12    template<class M>
    2.13    class XMap 
    2.14    {
    2.15 -    M &_map;
    2.16 +    typename SmartReference<M>::Type _map;
    2.17    public:
    2.18 +    typedef True NeedCopy;
    2.19 +
    2.20      typedef typename M::Value::Value Value;
    2.21      typedef typename M::Key Key;
    2.22      ///\e
    2.23 -    XMap(M &map) : _map(map) {}
    2.24 +    XMap(typename SmartParameter<M>::Type map) : _map(map) {}
    2.25      Value operator[](Key k) const {return _map[k].x;}
    2.26      void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
    2.27    };
    2.28 @@ -372,6 +375,12 @@
    2.29      return XMap<M>(m);
    2.30    }
    2.31  
    2.32 +  template<class M> 
    2.33 +  inline XMap<M> xMap(const M &m) 
    2.34 +  {
    2.35 +    return XMap<M>(m);
    2.36 +  }
    2.37 +
    2.38    ///Constant (read only) version of \ref XMap
    2.39  
    2.40    ///\ingroup maps
    2.41 @@ -379,8 +388,10 @@
    2.42    template<class M>
    2.43    class ConstXMap 
    2.44    {
    2.45 -    const M &_map;
    2.46 +    typename SmartConstReference<M>::Type _map;
    2.47    public:
    2.48 +    typedef True NeedCopy;
    2.49 +
    2.50      typedef typename M::Value::Value Value;
    2.51      typedef typename M::Key Key;
    2.52      ///\e
    2.53 @@ -407,12 +418,14 @@
    2.54    template<class M>
    2.55    class YMap 
    2.56    {
    2.57 -    M &_map;
    2.58 +    typename SmartReference<M>::Type _map;
    2.59    public:
    2.60 +    typedef True NeedCopy;
    2.61 +
    2.62      typedef typename M::Value::Value Value;
    2.63      typedef typename M::Key Key;
    2.64      ///\e
    2.65 -    YMap(M &map) : _map(map) {}
    2.66 +    YMap(typename SmartParameter<M>::Type map) : _map(map) {}
    2.67      Value operator[](Key k) const {return _map[k].y;}
    2.68      void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
    2.69    };
    2.70 @@ -429,6 +442,12 @@
    2.71      return YMap<M>(m);
    2.72    }
    2.73  
    2.74 +  template<class M> 
    2.75 +  inline YMap<M> yMap(const M &m) 
    2.76 +  {
    2.77 +    return YMap<M>(m);
    2.78 +  }
    2.79 +
    2.80    ///Constant (read only) version of \ref YMap
    2.81  
    2.82    ///\ingroup maps
    2.83 @@ -436,8 +455,10 @@
    2.84    template<class M>
    2.85    class ConstYMap 
    2.86    {
    2.87 -    const M &_map;
    2.88 +    typename SmartConstReference<M>::Type _map;
    2.89    public:
    2.90 +    typedef True NeedCopy;
    2.91 +
    2.92      typedef typename M::Value::Value Value;
    2.93      typedef typename M::Key Key;
    2.94      ///\e
    2.95 @@ -466,8 +487,10 @@
    2.96    template<class M>
    2.97    class NormSquareMap 
    2.98    {
    2.99 -    const M &_map;
   2.100 +    typename SmartConstReference<M>::Type _map;
   2.101    public:
   2.102 +    typedef True NeedCopy;
   2.103 +
   2.104      typedef typename M::Value::Value Value;
   2.105      typedef typename M::Key Key;
   2.106      ///\e