src/lemon/concept/maps.h
changeset 989 ca95f8b5c931
parent 987 87f7c54892df
child 1042 9c5daf8e3e32
     1.1 --- a/src/lemon/concept/maps.h	Sat Nov 13 17:47:44 2004 +0000
     1.2 +++ b/src/lemon/concept/maps.h	Sat Nov 13 21:37:54 2004 +0000
     1.3 @@ -40,11 +40,28 @@
     1.4        /// Map's value type. (The type of objects associated with the keys).
     1.5        typedef T Value;
     1.6  
     1.7 +      // \bug Value don't need to be default constructible.
     1.8        /// Returns the value associated with a key.
     1.9 -      Value operator[](const Key &k) const {return Value();}
    1.10 +      Value operator[](const Key &) const {return Value();}
    1.11  
    1.12 -      ///Default constructor
    1.13 -      ReadMap() {}
    1.14 +      template<typename _ReadMap>
    1.15 +      struct Constraints {
    1.16 +
    1.17 +	void constraints() {
    1.18 +	  Value val = m[key];
    1.19 +	  val = m[key];
    1.20 +	  typename _ReadMap::Value own_val = m[own_key]; 
    1.21 +	  own_val = m[own_key]; 
    1.22 +
    1.23 +	  ignore_unused_variable_warning(val);
    1.24 +	  ignore_unused_variable_warning(own_val);
    1.25 +	  ignore_unused_variable_warning(key);
    1.26 +	}
    1.27 +	Key& key;
    1.28 +	typename _ReadMap::Key& own_key;
    1.29 +	_ReadMap& m;
    1.30 +      };
    1.31 +      
    1.32      };
    1.33  
    1.34  
    1.35 @@ -63,6 +80,26 @@
    1.36  
    1.37        ///Default constructor
    1.38        WriteMap() {}
    1.39 +
    1.40 +      template <typename _WriteMap>
    1.41 +      struct Constraints {
    1.42 +	void constraints() {
    1.43 +	  // No constraints for constructor.
    1.44 +	  m.set(key, val);
    1.45 +	  m.set(own_key, own_val);
    1.46 +	  ignore_unused_variable(key);
    1.47 +	  ignore_unused_variable(val);
    1.48 +	  ignore_unused_variable(own_key);
    1.49 +	  ignore_unused_variable(own_val);
    1.50 +	}
    1.51 +
    1.52 +	Value& val;
    1.53 +	typename _WriteMap::Value own_val;
    1.54 +	Key& key;
    1.55 +	typename _WriteMap::Key& own_key;
    1.56 +	WriteMap& m;
    1.57 +
    1.58 +      };
    1.59      };
    1.60  
    1.61      ///Read/Writable map concept
    1.62 @@ -81,13 +118,18 @@
    1.63        /// Sets the value associated with a key.
    1.64        void set(const Key &k,const Value &t) {}
    1.65  
    1.66 -      ///Default constructor
    1.67 -      ReadWriteMap() {}
    1.68 +      template<typename _ReadWriteMap>
    1.69 +      struct Constraints {
    1.70 +	void constraints() {
    1.71 +	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
    1.72 +	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
    1.73 +	}
    1.74 +      };
    1.75      };
    1.76    
    1.77    
    1.78      ///Dereferable map concept
    1.79 -    template<typename K, typename T>
    1.80 +    template<typename K, typename T, typename R, typename CR>
    1.81      class ReferenceMap : public ReadWriteMap<K,T>
    1.82      {
    1.83      public:
    1.84 @@ -95,13 +137,14 @@
    1.85        typedef K Key;    
    1.86        /// Map's value type. (The type of objects associated with the keys).
    1.87        typedef T Value;
    1.88 +      /// Map's reference type.
    1.89 +      typedef R Reference;
    1.90 +      /// Map's const reference type.
    1.91 +      typedef CR ConstReference;
    1.92  
    1.93      protected:
    1.94        Value tmp;
    1.95      public:
    1.96 -      typedef Value& Reference;
    1.97 -      /// Map's const reference type.
    1.98 -      typedef const Value& ConstReference;
    1.99  
   1.100        ///Returns a reference to the value associated to a key.
   1.101        Reference operator[](const Key &i) { return tmp; }
   1.102 @@ -111,134 +154,32 @@
   1.103        /// Sets the value associated with a key.
   1.104        void set(const Key &k,const Value &t) { operator[](k)=t; }
   1.105  
   1.106 -      ///Default constructor
   1.107 -      ReferenceMap() {}
   1.108 +      // \todo rethink this concept
   1.109 +      template<typename _ReferenceMap>
   1.110 +      struct ReferenceMapConcept {
   1.111 +
   1.112 +	void constraints() {
   1.113 +	  checkConcept<ReadWriteMap, _ReferenceMap >();
   1.114 +	  m[key] = val;
   1.115 +	  val  = m[key];
   1.116 +	  m[key] = ref;
   1.117 +	  ref = m[key];
   1.118 +	  m[own_key] = own_val;
   1.119 +	  own_val  = m[own_key];
   1.120 +	  m[own_key] = own_ref;
   1.121 +	  own_ref = m[own_key];	  	  
   1.122 +	}
   1.123 +
   1.124 +	typename _ReferenceMap::Key& own_key;
   1.125 +	typename _ReferenceMap::Value& own_val;
   1.126 +	typename _ReferenceMap::Reference& own_ref;
   1.127 +	Key& key;
   1.128 +	Value& val;
   1.129 +	Reference& ref;
   1.130 +	ReferenceMap& m;
   1.131 +      };
   1.132      };
   1.133  
   1.134 -
   1.135 -    template<typename Item, typename T, typename Graph>
   1.136 -    class GraphMap : public ReadWriteMap<Item, T> {
   1.137 -      // I really, really don't like the idea that every graph should have
   1.138 -      // reference maps! --klao
   1.139 -
   1.140 -    private:
   1.141 -      // We state explicitly that graph maps have no default constructor?
   1.142 -      GraphMap();
   1.143 -
   1.144 -    public:
   1.145 -      explicit GraphMap(Graph const&) {}
   1.146 -      // value for initializing
   1.147 -      GraphMap(Graph const&, T) {}
   1.148 -
   1.149 -      // this probably should be required:
   1.150 -      GraphMap(GraphMap const&) {}
   1.151 -      GraphMap& operator=(GraphMap const&) { return *this; }
   1.152 -
   1.153 -      // but this is a absolute no-op! We should provide a more generic
   1.154 -      // graph-map-copy operation.
   1.155 -      //
   1.156 -      // template<typename TT>
   1.157 -      // GraphMap(GraphMap<TT> const&);
   1.158 -      //
   1.159 -      // template<typename TT>
   1.160 -      // GraphMap& operator=(const GraphMap<TT>&);
   1.161 -    };
   1.162 -
   1.163 -
   1.164 -    /****************  Concept-checking classes  ****************/
   1.165 -
   1.166 -    template<typename ReadMap>
   1.167 -    struct ReadMapConcept {
   1.168 -      typedef typename ReadMap::Key Key;
   1.169 -      typedef typename ReadMap::Value Value;
   1.170 -
   1.171 -      void constraints() {
   1.172 -	// No constraints for constructor.
   1.173 -
   1.174 -	// What are the requirement for the Value?
   1.175 -	// CopyConstructible? Assignable? None of these?
   1.176 -	Value v = m[k];
   1.177 -	v = m[k];
   1.178 -
   1.179 -	// FIXME:
   1.180 -	ignore_unused_variable_warning(v);
   1.181 -      }
   1.182 -
   1.183 -      ReadMap m;
   1.184 -      Key k;
   1.185 -    };
   1.186 -
   1.187 -    template<typename WriteMap>
   1.188 -    struct WriteMapConcept {
   1.189 -      typedef typename WriteMap::Key Key;
   1.190 -      typedef typename WriteMap::Value Value;
   1.191 -
   1.192 -      void constraints() {
   1.193 -	// No constraints for constructor.
   1.194 -
   1.195 -	m.set(k, v);
   1.196 -      }
   1.197 -
   1.198 -      WriteMap m;
   1.199 -      Key k;
   1.200 -      Value v;
   1.201 -    };
   1.202 -
   1.203 -    template<typename ReadWriteMap>
   1.204 -    struct ReadWriteMapConcept {
   1.205 -      void constraints() {
   1.206 -	function_requires< ReadMapConcept<ReadWriteMap> >();
   1.207 -	function_requires< WriteMapConcept<ReadWriteMap> >();
   1.208 -      }
   1.209 -    };
   1.210 -
   1.211 -    template<typename ReferenceMap>
   1.212 -    struct ReferenceMapConcept {
   1.213 -      typedef typename ReferenceMap::Key Key;
   1.214 -      typedef typename ReferenceMap::Value Value;
   1.215 -      typedef typename ReferenceMap::Reference Reference;
   1.216 -
   1.217 -      // What for is this?
   1.218 -      typedef typename ReferenceMap::ConstReference ConstReference;
   1.219 -
   1.220 -      void constraints() {
   1.221 -	function_requires< ReadWriteMapConcept<ReferenceMap> >();
   1.222 -
   1.223 -	m[k] = v;
   1.224 -	// Or should we require real reference?
   1.225 -	// Like this:
   1.226 -	// Value &vv = m[k];
   1.227 -	// ignore_unused_variable_warning(vv);
   1.228 -      }
   1.229 -
   1.230 -      ReferenceMap m;
   1.231 -      Key k;
   1.232 -      Value v;
   1.233 -    };
   1.234 -
   1.235 -    /// \todo GraphMapConceptCheck
   1.236 -
   1.237 -    template<typename GraphMap, typename Graph>
   1.238 -    struct GraphMapConcept {
   1.239 -      void constraints() {
   1.240 -	function_requires< ReadWriteMapConcept<GraphMap> >();
   1.241 -	// Construction with a graph parameter
   1.242 -	GraphMap a(g);
   1.243 -	// Ctor with a graph and a default value parameter
   1.244 -	GraphMap a2(g,t);
   1.245 -	// Copy ctor. Do we need it?
   1.246 -	GraphMap b=c;
   1.247 -	// Copy operator. Do we need it?
   1.248 -	a=b;
   1.249 -
   1.250 -	ignore_unused_variable_warning(a2);
   1.251 -      }
   1.252 -      const GraphMap &c;
   1.253 -      const Graph &g;
   1.254 -      const typename GraphMap::Value &t;
   1.255 -    };
   1.256 -    
   1.257 -
   1.258      // @}
   1.259  
   1.260    } //namespace concept