lemon/concepts/maps.h
changeset 65 bfbc57a51fbb
parent 25 751cd8f9bb1c
child 35 f8ddf1b1541a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/concepts/maps.h	Wed Feb 06 10:52:58 2008 +0000
     1.3 @@ -0,0 +1,207 @@
     1.4 +/* -*- C++ -*-
     1.5 + *
     1.6 + * This file is a part of LEMON, a generic C++ optimization library
     1.7 + *
     1.8 + * Copyright (C) 2003-2007
     1.9 + * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 + *
    1.12 + * Permission to use, modify and distribute this software is granted
    1.13 + * provided that this copyright notice appears in all copies. For
    1.14 + * precise terms see the accompanying LICENSE file.
    1.15 + *
    1.16 + * This software is provided "AS IS" with no warranty of any kind,
    1.17 + * express or implied, and with no claim as to its suitability for any
    1.18 + * purpose.
    1.19 + *
    1.20 + */
    1.21 +
    1.22 +#ifndef LEMON_CONCEPT_MAPS_H
    1.23 +#define LEMON_CONCEPT_MAPS_H
    1.24 +
    1.25 +#include <lemon/bits/utility.h>
    1.26 +#include <lemon/concept_check.h>
    1.27 +
    1.28 +///\ingroup concept
    1.29 +///\file
    1.30 +///\brief Map concepts checking classes for testing and documenting.
    1.31 +
    1.32 +namespace lemon {
    1.33 +
    1.34 +  namespace concepts {
    1.35 +  
    1.36 +    /// \addtogroup concept
    1.37 +    /// @{
    1.38 +
    1.39 +    /// Readable map concept
    1.40 +
    1.41 +    /// Readable map concept.
    1.42 +    ///
    1.43 +    template<typename K, typename T>
    1.44 +    class ReadMap
    1.45 +    {
    1.46 +    public:
    1.47 +      /// Map's key type.
    1.48 +      typedef K Key;    
    1.49 +      /// Map's value type. (The type of objects associated with the keys).
    1.50 +      typedef T Value;
    1.51 +
    1.52 +      /// Returns the value associated with a key.
    1.53 +
    1.54 +      /// \bug Value shouldn't need to be default constructible.
    1.55 +      ///
    1.56 +      Value operator[](const Key &) const {return Value();}
    1.57 +
    1.58 +      template<typename _ReadMap>
    1.59 +      struct Constraints {
    1.60 +
    1.61 +	void constraints() {
    1.62 +	  Value val = m[key];
    1.63 +	  val = m[key];
    1.64 +	  typename _ReadMap::Value own_val = m[own_key]; 
    1.65 +	  own_val = m[own_key]; 
    1.66 +
    1.67 +	  ignore_unused_variable_warning(val);
    1.68 +	  ignore_unused_variable_warning(own_val);
    1.69 +	  ignore_unused_variable_warning(key);
    1.70 +	}
    1.71 +	Key& key;
    1.72 +	typename _ReadMap::Key& own_key;
    1.73 +	_ReadMap& m;
    1.74 +      };
    1.75 +      
    1.76 +    };
    1.77 +
    1.78 +
    1.79 +    /// Writable map concept
    1.80 +    
    1.81 +    /// Writable map concept.
    1.82 +    ///
    1.83 +    template<typename K, typename T>
    1.84 +    class WriteMap
    1.85 +    {
    1.86 +    public:
    1.87 +      /// Map's key type.
    1.88 +      typedef K Key;    
    1.89 +      /// Map's value type. (The type of objects associated with the keys).
    1.90 +      typedef T Value;
    1.91 +
    1.92 +      /// Sets the value associated with a key.
    1.93 +      void set(const Key &,const Value &) {}
    1.94 +
    1.95 +      ///Default constructor
    1.96 +      WriteMap() {}
    1.97 +
    1.98 +      template <typename _WriteMap>
    1.99 +      struct Constraints {
   1.100 +	void constraints() {
   1.101 +	  // No constraints for constructor.
   1.102 +	  m.set(key, val);
   1.103 +	  m.set(own_key, own_val);
   1.104 +	  ignore_unused_variable_warning(key);
   1.105 +	  ignore_unused_variable_warning(val);
   1.106 +	  ignore_unused_variable_warning(own_key);
   1.107 +	  ignore_unused_variable_warning(own_val);
   1.108 +	}
   1.109 +
   1.110 +	Value& val;
   1.111 +	typename _WriteMap::Value own_val;
   1.112 +	Key& key;
   1.113 +	typename _WriteMap::Key& own_key;
   1.114 +	_WriteMap& m;
   1.115 +
   1.116 +      };
   1.117 +    };
   1.118 +
   1.119 +    /// Read/Writable map concept
   1.120 +    
   1.121 +    /// Read/writable map concept.
   1.122 +    ///
   1.123 +    template<typename K, typename T>
   1.124 +    class ReadWriteMap : public ReadMap<K,T>,
   1.125 +			    public WriteMap<K,T>
   1.126 +    {
   1.127 +    public:
   1.128 +      /// Map's key type.
   1.129 +      typedef K Key;    
   1.130 +      /// Map's value type. (The type of objects associated with the keys).
   1.131 +      typedef T Value;
   1.132 +
   1.133 +      /// Returns the value associated with a key.
   1.134 +      Value operator[](const Key &) const {return Value();}
   1.135 +      /// Sets the value associated with a key.
   1.136 +      void set(const Key & ,const Value &) {}
   1.137 +
   1.138 +      template<typename _ReadWriteMap>
   1.139 +      struct Constraints {
   1.140 +	void constraints() {
   1.141 +	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   1.142 +	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   1.143 +	}
   1.144 +      };
   1.145 +    };
   1.146 +  
   1.147 +  
   1.148 +    /// Dereferable map concept
   1.149 +    
   1.150 +    /// Dereferable map concept.
   1.151 +    ///
   1.152 +    template<typename K, typename T, typename R, typename CR>
   1.153 +    class ReferenceMap : public ReadWriteMap<K,T>
   1.154 +    {
   1.155 +    public:
   1.156 +      /// Tag for reference maps.
   1.157 +      typedef True ReferenceMapTag;
   1.158 +      /// Map's key type.
   1.159 +      typedef K Key;    
   1.160 +      /// Map's value type. (The type of objects associated with the keys).
   1.161 +      typedef T Value;
   1.162 +      /// Map's reference type.
   1.163 +      typedef R Reference;
   1.164 +      /// Map's const reference type.
   1.165 +      typedef CR ConstReference;
   1.166 +
   1.167 +    protected:
   1.168 +      Value tmp;
   1.169 +    public:
   1.170 +
   1.171 +      ///Returns a reference to the value associated to a key.
   1.172 +      Reference operator[](const Key &) { return tmp; }
   1.173 +      ///Returns a const reference to the value associated to a key.
   1.174 +      ConstReference operator[](const Key &) const { return tmp; }
   1.175 +      /// Sets the value associated with a key.
   1.176 +      void set(const Key &k,const Value &t) { operator[](k)=t; }
   1.177 +
   1.178 +      /// \todo Rethink this concept. 
   1.179 +      template<typename _ReferenceMap>
   1.180 +      struct ReferenceMapConcept {
   1.181 +
   1.182 +	void constraints() {
   1.183 +	  checkConcept<ReadWriteMap, _ReferenceMap >();
   1.184 +	  m[key] = val;
   1.185 +	  val  = m[key];
   1.186 +	  m[key] = ref;
   1.187 +	  ref = m[key];
   1.188 +	  m[own_key] = own_val;
   1.189 +	  own_val  = m[own_key];
   1.190 +	  m[own_key] = own_ref;
   1.191 +	  own_ref = m[own_key];	  	  
   1.192 +	}
   1.193 +
   1.194 +	typename _ReferenceMap::Key& own_key;
   1.195 +	typename _ReferenceMap::Value& own_val;
   1.196 +	typename _ReferenceMap::Reference& own_ref;
   1.197 +	Key& key;
   1.198 +	Value& val;
   1.199 +	Reference& ref;
   1.200 +	_ReferenceMap& m;
   1.201 +      };
   1.202 +    };
   1.203 +
   1.204 +    // @}
   1.205 +
   1.206 +  } //namespace concepts
   1.207 +
   1.208 +} //namespace lemon
   1.209 +
   1.210 +#endif // LEMON_CONCEPT_MAPS_H