lemon/concepts/maps.h
changeset 80 15968e25ca08
parent 74 9394072da54f
child 94 a4688e4138ec
equal deleted inserted replaced
6:f7e467d44b8e 7:8e0bf057246b
    27 ///\brief Map concepts checking classes for testing and documenting.
    27 ///\brief Map concepts checking classes for testing and documenting.
    28 
    28 
    29 namespace lemon {
    29 namespace lemon {
    30 
    30 
    31   namespace concepts {
    31   namespace concepts {
    32   
    32 
    33     /// \addtogroup concept
    33     /// \addtogroup concept
    34     /// @{
    34     /// @{
    35 
    35 
    36     /// Readable map concept
    36     /// Readable map concept
    37 
    37 
    40     template<typename K, typename T>
    40     template<typename K, typename T>
    41     class ReadMap
    41     class ReadMap
    42     {
    42     {
    43     public:
    43     public:
    44       /// The key type of the map.
    44       /// The key type of the map.
    45       typedef K Key;    
    45       typedef K Key;
    46       /// The value type of the map. (The type of objects associated with the keys).
    46       /// The value type of the map. (The type of objects associated with the keys).
    47       typedef T Value;
    47       typedef T Value;
    48 
    48 
    49       /// Returns the value associated with a key.
    49       /// Returns the value associated with the given key.
    50 
    50 
    51       /// Returns the value associated with a key.
    51       /// Returns the value associated with the given key.
    52       /// \bug Value shouldn't need to be default constructible.
    52       /// \bug Value shouldn't need to be default constructible. 
    53       ///
    53       Value operator[](const Key &) const { return Value(); }
    54       Value operator[](const Key &) const {return Value();}
       
    55 
    54 
    56       template<typename _ReadMap>
    55       template<typename _ReadMap>
    57       struct Constraints {
    56       struct Constraints {
    58 	void constraints() {
    57 	void constraints() {
    59 	  Value val = m[key];
    58 	  Value val = m[key];
    60 	  val = m[key];
    59 	  val = m[key];
    61 	  typename _ReadMap::Value own_val = m[own_key]; 
    60 	  typename _ReadMap::Value own_val = m[own_key];
    62 	  own_val = m[own_key]; 
    61 	  own_val = m[own_key];
    63 
    62 
    64 	  ignore_unused_variable_warning(val);
       
    65 	  ignore_unused_variable_warning(own_val);
       
    66 	  ignore_unused_variable_warning(key);
       
    67 	}
       
    68 	Key& key;
       
    69 	typename _ReadMap::Key& own_key;
       
    70 	_ReadMap& m;
       
    71       };
       
    72       
       
    73     };
       
    74 
       
    75 
       
    76     /// Writable map concept
       
    77     
       
    78     /// Writable map concept.
       
    79     ///
       
    80     template<typename K, typename T>
       
    81     class WriteMap
       
    82     {
       
    83     public:
       
    84       /// The key type of the map.
       
    85       typedef K Key;    
       
    86       /// The value type of the map. (The type of objects associated with the keys).
       
    87       typedef T Value;
       
    88 
       
    89       /// Sets the value associated with a key.
       
    90       void set(const Key &,const Value &) {}
       
    91 
       
    92       ///Default constructor
       
    93       WriteMap() {}
       
    94 
       
    95       template <typename _WriteMap>
       
    96       struct Constraints {
       
    97 	void constraints() {
       
    98 	  // No constraints for constructor.
       
    99 	  m.set(key, val);
       
   100 	  m.set(own_key, own_val);
       
   101 	  ignore_unused_variable_warning(key);
    63 	  ignore_unused_variable_warning(key);
   102 	  ignore_unused_variable_warning(val);
    64 	  ignore_unused_variable_warning(val);
   103 	  ignore_unused_variable_warning(own_key);
    65 	  ignore_unused_variable_warning(own_key);
   104 	  ignore_unused_variable_warning(own_val);
    66 	  ignore_unused_variable_warning(own_val);
   105 	}
    67 	}
   106 
    68 	const Key& key;
   107 	Value& val;
    69 	const typename _ReadMap::Key& own_key;
   108 	typename _WriteMap::Value own_val;
    70 	const _ReadMap& m;
   109 	Key& key;
    71       };
   110 	typename _WriteMap::Key& own_key;
    72 
       
    73     };
       
    74 
       
    75 
       
    76     /// Writable map concept
       
    77 
       
    78     /// Writable map concept.
       
    79     ///
       
    80     template<typename K, typename T>
       
    81     class WriteMap
       
    82     {
       
    83     public:
       
    84       /// The key type of the map.
       
    85       typedef K Key;
       
    86       /// The value type of the map. (The type of objects associated with the keys).
       
    87       typedef T Value;
       
    88 
       
    89       /// Sets the value associated with the given key.
       
    90       void set(const Key &, const Value &) {}
       
    91 
       
    92       /// Default constructor.
       
    93       WriteMap() {}
       
    94 
       
    95       template <typename _WriteMap>
       
    96       struct Constraints {
       
    97 	void constraints() {
       
    98 	  m.set(key, val);
       
    99 	  m.set(own_key, own_val);
       
   100 
       
   101 	  ignore_unused_variable_warning(key);
       
   102 	  ignore_unused_variable_warning(val);
       
   103 	  ignore_unused_variable_warning(own_key);
       
   104 	  ignore_unused_variable_warning(own_val);
       
   105 	}
       
   106 	const Key& key;
       
   107 	const Value& val;
       
   108 	const typename _WriteMap::Key& own_key;
       
   109 	const typename _WriteMap::Value own_val;
   111 	_WriteMap& m;
   110 	_WriteMap& m;
   112 
       
   113       };
   111       };
   114     };
   112     };
   115 
   113 
   116     /// Read/writable map concept
   114     /// Read/writable map concept
   117     
   115 
   118     /// Read/writable map concept.
   116     /// Read/writable map concept.
   119     ///
   117     ///
   120     template<typename K, typename T>
   118     template<typename K, typename T>
   121     class ReadWriteMap : public ReadMap<K,T>,
   119     class ReadWriteMap : public ReadMap<K,T>,
   122 			 public WriteMap<K,T>
   120 			 public WriteMap<K,T>
   123     {
   121     {
   124     public:
   122     public:
   125       /// The key type of the map.
   123       /// The key type of the map.
   126       typedef K Key;    
   124       typedef K Key;
   127       /// The value type of the map. (The type of objects associated with the keys).
   125       /// The value type of the map. (The type of objects associated with the keys).
   128       typedef T Value;
   126       typedef T Value;
   129 
   127 
   130       /// Returns the value associated with a key.
   128       /// Returns the value associated with the given key.
   131       Value operator[](const Key &) const {return Value();}
   129       Value operator[](const Key &) const { return Value(); }
   132       /// Sets the value associated with a key.
   130 
   133       void set(const Key & ,const Value &) {}
   131       /// Sets the value associated with the given key.
       
   132       void set(const Key &, const Value &) {}
   134 
   133 
   135       template<typename _ReadWriteMap>
   134       template<typename _ReadWriteMap>
   136       struct Constraints {
   135       struct Constraints {
   137 	void constraints() {
   136 	void constraints() {
   138 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   137 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   139 	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   138 	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   140 	}
   139 	}
   141       };
   140       };
   142     };
   141     };
   143   
   142 
   144   
   143 
   145     /// Dereferable map concept
   144     /// Dereferable map concept
   146     
   145 
   147     /// Dereferable map concept.
   146     /// Dereferable map concept.
   148     ///
   147     ///
   149     /// \todo Rethink this concept.
       
   150     template<typename K, typename T, typename R, typename CR>
   148     template<typename K, typename T, typename R, typename CR>
   151     class ReferenceMap : public ReadWriteMap<K,T>
   149     class ReferenceMap : public ReadWriteMap<K,T>
   152     {
   150     {
   153     public:
   151     public:
   154       /// Tag for reference maps.
   152       /// Tag for reference maps.
   155       typedef True ReferenceMapTag;
   153       typedef True ReferenceMapTag;
   156       /// The key type of the map.
   154       /// The key type of the map.
   157       typedef K Key;    
   155       typedef K Key;
   158       /// The value type of the map. (The type of objects associated with the keys).
   156       /// The value type of the map. (The type of objects associated with the keys).
   159       typedef T Value;
   157       typedef T Value;
   160       /// The reference type of the map.
   158       /// The reference type of the map.
   161       typedef R Reference;
   159       typedef R Reference;
   162       /// The const reference type of the map.
   160       /// The const reference type of the map.
   164 
   162 
   165     protected:
   163     protected:
   166       Value tmp;
   164       Value tmp;
   167     public:
   165     public:
   168 
   166 
   169       ///Returns a reference to the value associated with a key.
   167       /// Returns a reference to the value associated with the given key.
   170       Reference operator[](const Key &) { return tmp; }
   168       Reference operator[](const Key &) { return tmp; }
   171       ///Returns a const reference to the value associated with a key.
   169 
       
   170       /// Returns a const reference to the value associated with the given key.
   172       ConstReference operator[](const Key &) const { return tmp; }
   171       ConstReference operator[](const Key &) const { return tmp; }
   173       /// Sets the value associated with a key.
   172 
       
   173       /// Sets the value associated with the given key.
   174       void set(const Key &k,const Value &t) { operator[](k)=t; }
   174       void set(const Key &k,const Value &t) { operator[](k)=t; }
   175 
   175 
   176       template<typename _ReferenceMap>
   176       template<typename _ReferenceMap>
   177       struct Constraints {
   177       struct Constraints {
   178 	void constraints() {
   178 	void constraints() {
   179 	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
   179 	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
       
   180 	  ref = m[key];
   180 	  m[key] = val;
   181 	  m[key] = val;
   181 	  val  = m[key];
       
   182 	  m[key] = ref;
   182 	  m[key] = ref;
   183 	  ref = m[key];
   183 	  m[key] = cref;
       
   184 	  own_ref = m[own_key];
   184 	  m[own_key] = own_val;
   185 	  m[own_key] = own_val;
   185 	  own_val  = m[own_key];
       
   186 	  m[own_key] = own_ref;
   186 	  m[own_key] = own_ref;
   187 	  own_ref = m[own_key];	  	  
   187 	  m[own_key] = own_cref;
   188 	}
   188 	  m[key] = m[own_key];
   189 
   189 	  m[own_key] = m[key];
   190 	typename _ReferenceMap::Key& own_key;
   190 	}
       
   191 	const Key& key;
       
   192 	Value& val;
       
   193 	Reference ref;
       
   194 	ConstReference cref;
       
   195 	const typename _ReferenceMap::Key& own_key;
   191 	typename _ReferenceMap::Value& own_val;
   196 	typename _ReferenceMap::Value& own_val;
   192 	typename _ReferenceMap::Reference own_ref;
   197 	typename _ReferenceMap::Reference own_ref;
   193 	Key& key;
   198 	typename _ReferenceMap::ConstReference own_cref;
   194 	Value& val;
       
   195 	Reference ref;
       
   196 	_ReferenceMap& m;
   199 	_ReferenceMap& m;
   197       };
   200       };
   198     };
   201     };
   199 
   202 
   200     // @}
   203     // @}