More exact concept checking for map concepts.
authorPeter Kovacs <kpeter@inf.elte.hu>
Sat, 15 Mar 2008 20:21:21 +0100
changeset 79d73c2e8b25cb
parent 76 fc178a057bbd
child 80 15968e25ca08
More exact concept checking for map concepts.
lemon/concepts/maps.h
     1.1 --- a/lemon/concepts/maps.h	Tue Feb 12 21:08:29 2008 +0000
     1.2 +++ b/lemon/concepts/maps.h	Sat Mar 15 20:21:21 2008 +0100
     1.3 @@ -29,7 +29,7 @@
     1.4  namespace lemon {
     1.5  
     1.6    namespace concepts {
     1.7 -  
     1.8 +
     1.9      /// \addtogroup concept
    1.10      /// @{
    1.11  
    1.12 @@ -42,39 +42,39 @@
    1.13      {
    1.14      public:
    1.15        /// The key type of the map.
    1.16 -      typedef K Key;    
    1.17 +      typedef K Key;
    1.18        /// The value type of the map. (The type of objects associated with the keys).
    1.19        typedef T Value;
    1.20  
    1.21 -      /// Returns the value associated with a key.
    1.22 +      /// Returns the value associated with the given key.
    1.23  
    1.24 -      /// Returns the value associated with a key.
    1.25 -      /// \bug Value shouldn't need to be default constructible.
    1.26 -      ///
    1.27 -      Value operator[](const Key &) const {return Value();}
    1.28 +      /// Returns the value associated with the given key.
    1.29 +      /// \bug Value shouldn't need to be default constructible. 
    1.30 +      Value operator[](const Key &) const { return Value(); }
    1.31  
    1.32        template<typename _ReadMap>
    1.33        struct Constraints {
    1.34  	void constraints() {
    1.35  	  Value val = m[key];
    1.36  	  val = m[key];
    1.37 -	  typename _ReadMap::Value own_val = m[own_key]; 
    1.38 -	  own_val = m[own_key]; 
    1.39 +	  typename _ReadMap::Value own_val = m[own_key];
    1.40 +	  own_val = m[own_key];
    1.41  
    1.42 +	  ignore_unused_variable_warning(key);
    1.43  	  ignore_unused_variable_warning(val);
    1.44 +	  ignore_unused_variable_warning(own_key);
    1.45  	  ignore_unused_variable_warning(own_val);
    1.46 -	  ignore_unused_variable_warning(key);
    1.47  	}
    1.48 -	Key& key;
    1.49 -	typename _ReadMap::Key& own_key;
    1.50 -	_ReadMap& m;
    1.51 +	const Key& key;
    1.52 +	const typename _ReadMap::Key& own_key;
    1.53 +	const _ReadMap& m;
    1.54        };
    1.55 -      
    1.56 +
    1.57      };
    1.58  
    1.59  
    1.60      /// Writable map concept
    1.61 -    
    1.62 +
    1.63      /// Writable map concept.
    1.64      ///
    1.65      template<typename K, typename T>
    1.66 @@ -82,39 +82,37 @@
    1.67      {
    1.68      public:
    1.69        /// The key type of the map.
    1.70 -      typedef K Key;    
    1.71 +      typedef K Key;
    1.72        /// The value type of the map. (The type of objects associated with the keys).
    1.73        typedef T Value;
    1.74  
    1.75 -      /// Sets the value associated with a key.
    1.76 -      void set(const Key &,const Value &) {}
    1.77 +      /// Sets the value associated with the given key.
    1.78 +      void set(const Key &, const Value &) {}
    1.79  
    1.80 -      ///Default constructor
    1.81 +      /// Default constructor.
    1.82        WriteMap() {}
    1.83  
    1.84        template <typename _WriteMap>
    1.85        struct Constraints {
    1.86  	void constraints() {
    1.87 -	  // No constraints for constructor.
    1.88  	  m.set(key, val);
    1.89  	  m.set(own_key, own_val);
    1.90 +
    1.91  	  ignore_unused_variable_warning(key);
    1.92  	  ignore_unused_variable_warning(val);
    1.93  	  ignore_unused_variable_warning(own_key);
    1.94  	  ignore_unused_variable_warning(own_val);
    1.95  	}
    1.96 -
    1.97 -	Value& val;
    1.98 -	typename _WriteMap::Value own_val;
    1.99 -	Key& key;
   1.100 -	typename _WriteMap::Key& own_key;
   1.101 +	const Key& key;
   1.102 +	const Value& val;
   1.103 +	const typename _WriteMap::Key& own_key;
   1.104 +	const typename _WriteMap::Value own_val;
   1.105  	_WriteMap& m;
   1.106 -
   1.107        };
   1.108      };
   1.109  
   1.110      /// Read/writable map concept
   1.111 -    
   1.112 +
   1.113      /// Read/writable map concept.
   1.114      ///
   1.115      template<typename K, typename T>
   1.116 @@ -123,14 +121,15 @@
   1.117      {
   1.118      public:
   1.119        /// The key type of the map.
   1.120 -      typedef K Key;    
   1.121 +      typedef K Key;
   1.122        /// The value type of the map. (The type of objects associated with the keys).
   1.123        typedef T Value;
   1.124  
   1.125 -      /// Returns the value associated with a key.
   1.126 -      Value operator[](const Key &) const {return Value();}
   1.127 -      /// Sets the value associated with a key.
   1.128 -      void set(const Key & ,const Value &) {}
   1.129 +      /// Returns the value associated with the given key.
   1.130 +      Value operator[](const Key &) const { return Value(); }
   1.131 +
   1.132 +      /// Sets the value associated with the given key.
   1.133 +      void set(const Key &, const Value &) {}
   1.134  
   1.135        template<typename _ReadWriteMap>
   1.136        struct Constraints {
   1.137 @@ -140,13 +139,12 @@
   1.138  	}
   1.139        };
   1.140      };
   1.141 -  
   1.142 -  
   1.143 +
   1.144 +
   1.145      /// Dereferable map concept
   1.146 -    
   1.147 +
   1.148      /// Dereferable map concept.
   1.149      ///
   1.150 -    /// \todo Rethink this concept.
   1.151      template<typename K, typename T, typename R, typename CR>
   1.152      class ReferenceMap : public ReadWriteMap<K,T>
   1.153      {
   1.154 @@ -154,7 +152,7 @@
   1.155        /// Tag for reference maps.
   1.156        typedef True ReferenceMapTag;
   1.157        /// The key type of the map.
   1.158 -      typedef K Key;    
   1.159 +      typedef K Key;
   1.160        /// The value type of the map. (The type of objects associated with the keys).
   1.161        typedef T Value;
   1.162        /// The reference type of the map.
   1.163 @@ -166,33 +164,38 @@
   1.164        Value tmp;
   1.165      public:
   1.166  
   1.167 -      ///Returns a reference to the value associated with a key.
   1.168 +      /// Returns a reference to the value associated with the given key.
   1.169        Reference operator[](const Key &) { return tmp; }
   1.170 -      ///Returns a const reference to the value associated with a key.
   1.171 +
   1.172 +      /// Returns a const reference to the value associated with the given key.
   1.173        ConstReference operator[](const Key &) const { return tmp; }
   1.174 -      /// Sets the value associated with a key.
   1.175 +
   1.176 +      /// Sets the value associated with the given key.
   1.177        void set(const Key &k,const Value &t) { operator[](k)=t; }
   1.178  
   1.179        template<typename _ReferenceMap>
   1.180        struct Constraints {
   1.181  	void constraints() {
   1.182  	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
   1.183 +	  ref = m[key];
   1.184  	  m[key] = val;
   1.185 -	  val  = m[key];
   1.186  	  m[key] = ref;
   1.187 -	  ref = m[key];
   1.188 +	  m[key] = cref;
   1.189 +	  own_ref = m[own_key];
   1.190  	  m[own_key] = own_val;
   1.191 -	  own_val  = m[own_key];
   1.192  	  m[own_key] = own_ref;
   1.193 -	  own_ref = m[own_key];	  	  
   1.194 +	  m[own_key] = own_cref;
   1.195 +	  m[key] = m[own_key];
   1.196 +	  m[own_key] = m[key];
   1.197  	}
   1.198 -
   1.199 -	typename _ReferenceMap::Key& own_key;
   1.200 +	const Key& key;
   1.201 +	Value& val;
   1.202 +	Reference ref;
   1.203 +	ConstReference cref;
   1.204 +	const typename _ReferenceMap::Key& own_key;
   1.205  	typename _ReferenceMap::Value& own_val;
   1.206  	typename _ReferenceMap::Reference own_ref;
   1.207 -	Key& key;
   1.208 -	Value& val;
   1.209 -	Reference ref;
   1.210 +	typename _ReferenceMap::ConstReference own_cref;
   1.211  	_ReferenceMap& m;
   1.212        };
   1.213      };