lemon/concepts/maps.h
changeset 2624 dc4dd5fc0e25
parent 2553 bfced05fa852
equal deleted inserted replaced
2:19e0d3a3940b 3:de979145ddc6
    32   
    32   
    33     /// \addtogroup concept
    33     /// \addtogroup concept
    34     /// @{
    34     /// @{
    35 
    35 
    36     /// Readable map concept
    36     /// Readable map concept
       
    37 
       
    38     /// Readable map concept.
       
    39     ///
    37     template<typename K, typename T>
    40     template<typename K, typename T>
    38     class ReadMap
    41     class ReadMap
    39     {
    42     {
    40     public:
    43     public:
    41       /// Map's key type.
    44       /// The key type of the map.
    42       typedef K Key;    
    45       typedef K Key;    
    43       /// Map's value type. (The type of objects associated with the keys).
    46       /// The value type of the map. (The type of objects associated with the keys).
    44       typedef T Value;
    47       typedef T Value;
    45 
    48 
    46       // \bug Value don't need to be default constructible.
       
    47       /// Returns the value associated with a key.
    49       /// Returns the value associated with a key.
       
    50 
       
    51       /// Returns the value associated with a key.
       
    52       /// \bug Value shouldn't need to be default constructible.
       
    53       ///
    48       Value operator[](const Key &) const {return Value();}
    54       Value operator[](const Key &) const {return Value();}
    49 
    55 
    50       template<typename _ReadMap>
    56       template<typename _ReadMap>
    51       struct Constraints {
    57       struct Constraints {
    52 
    58 
    67       
    73       
    68     };
    74     };
    69 
    75 
    70 
    76 
    71     /// Writable map concept
    77     /// Writable map concept
       
    78     
       
    79     /// Writable map concept.
       
    80     ///
    72     template<typename K, typename T>
    81     template<typename K, typename T>
    73     class WriteMap
    82     class WriteMap
    74     {
    83     {
    75     public:
    84     public:
    76       /// Map's key type.
    85       /// The key type of the map.
    77       typedef K Key;    
    86       typedef K Key;    
    78       /// Map's value type. (The type of objects associated with the keys).
    87       /// The value type of the map. (The type of objects associated with the keys).
    79       typedef T Value;
    88       typedef T Value;
    80 
    89 
    81       /// Sets the value associated with a key.
    90       /// Sets the value associated with a key.
    82       void set(const Key &,const Value &) {}
    91       void set(const Key &,const Value &) {}
    83 
    92 
   103 	_WriteMap& m;
   112 	_WriteMap& m;
   104 
   113 
   105       };
   114       };
   106     };
   115     };
   107 
   116 
   108     ///Read/Writable map concept
   117     /// Read/writable map concept
       
   118     
       
   119     /// Read/writable map concept.
       
   120     ///
   109     template<typename K, typename T>
   121     template<typename K, typename T>
   110     class ReadWriteMap : public ReadMap<K,T>,
   122     class ReadWriteMap : public ReadMap<K,T>,
   111 			    public WriteMap<K,T>
   123 			 public WriteMap<K,T>
   112     {
   124     {
   113     public:
   125     public:
   114       /// Map's key type.
   126       /// The key type of the map.
   115       typedef K Key;    
   127       typedef K Key;    
   116       /// Map's value type. (The type of objects associated with the keys).
   128       /// The value type of the map. (The type of objects associated with the keys).
   117       typedef T Value;
   129       typedef T Value;
   118 
   130 
   119       /// Returns the value associated with a key.
   131       /// Returns the value associated with a key.
   120       Value operator[](const Key &) const {return Value();}
   132       Value operator[](const Key &) const {return Value();}
   121       /// Sets the value associated with a key.
   133       /// Sets the value associated with a key.
   130       };
   142       };
   131     };
   143     };
   132   
   144   
   133   
   145   
   134     ///Dereferable map concept
   146     ///Dereferable map concept
       
   147     
       
   148     /// Dereferable map concept.
       
   149     ///
       
   150     /// \todo Rethink this concept.
   135     template<typename K, typename T, typename R, typename CR>
   151     template<typename K, typename T, typename R, typename CR>
   136     class ReferenceMap : public ReadWriteMap<K,T>
   152     class ReferenceMap : public ReadWriteMap<K,T>
   137     {
   153     {
   138     public:
   154     public:
   139       /// Tag for reference maps.
   155       /// Tag for reference maps.
   140       typedef True ReferenceMapTag;
   156       typedef True ReferenceMapTag;
   141       /// Map's key type.
   157       /// The key type of the map.
   142       typedef K Key;    
   158       typedef K Key;    
   143       /// Map's value type. (The type of objects associated with the keys).
   159       /// The value type of the map. (The type of objects associated with the keys).
   144       typedef T Value;
   160       typedef T Value;
   145       /// Map's reference type.
   161       /// The reference type of the map.
   146       typedef R Reference;
   162       typedef R Reference;
   147       /// Map's const reference type.
   163       /// The const reference type of the map.
   148       typedef CR ConstReference;
   164       typedef CR ConstReference;
   149 
   165 
   150     protected:
   166     protected:
   151       Value tmp;
   167       Value tmp;
   152     public:
   168     public:
   153 
   169 
   154       ///Returns a reference to the value associated to a key.
   170       ///Returns a reference to the value associated with a key.
   155       Reference operator[](const Key &) { return tmp; }
   171       Reference operator[](const Key &) { return tmp; }
   156       ///Returns a const reference to the value associated to a key.
   172       ///Returns a const reference to the value associated with a key.
   157       ConstReference operator[](const Key &) const
   173       ConstReference operator[](const Key &) const { return tmp; }
   158       { return tmp; }
       
   159       /// Sets the value associated with a key.
   174       /// Sets the value associated with a key.
   160       void set(const Key &k,const Value &t) { operator[](k)=t; }
   175       void set(const Key &k,const Value &t) { operator[](k)=t; }
   161 
   176 
   162       // \todo rethink this concept
       
   163       template<typename _ReferenceMap>
   177       template<typename _ReferenceMap>
   164       struct ReferenceMapConcept {
   178       struct Constraints {
   165 
   179 
   166 	void constraints() {
   180 	void constraints() {
   167 	  checkConcept<ReadWriteMap, _ReferenceMap >();
   181 	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
   168 	  m[key] = val;
   182 	  m[key] = val;
   169 	  val  = m[key];
   183 	  val  = m[key];
   170 	  m[key] = ref;
   184 	  m[key] = ref;
   171 	  ref = m[key];
   185 	  ref = m[key];
   172 	  m[own_key] = own_val;
   186 	  m[own_key] = own_val;
   175 	  own_ref = m[own_key];	  	  
   189 	  own_ref = m[own_key];	  	  
   176 	}
   190 	}
   177 
   191 
   178 	typename _ReferenceMap::Key& own_key;
   192 	typename _ReferenceMap::Key& own_key;
   179 	typename _ReferenceMap::Value& own_val;
   193 	typename _ReferenceMap::Value& own_val;
   180 	typename _ReferenceMap::Reference& own_ref;
   194 	typename _ReferenceMap::Reference own_ref;
   181 	Key& key;
   195 	Key& key;
   182 	Value& val;
   196 	Value& val;
   183 	Reference& ref;
   197 	Reference ref;
   184 	_ReferenceMap& m;
   198 	_ReferenceMap& m;
   185       };
   199       };
   186     };
   200     };
   187 
   201 
   188     // @}
   202     // @}
   189 
   203 
   190   } //namespace concepts
   204   } //namespace concepts
       
   205 
   191 } //namespace lemon
   206 } //namespace lemon
       
   207 
   192 #endif // LEMON_CONCEPT_MAPS_H
   208 #endif // LEMON_CONCEPT_MAPS_H