lemon/concepts/matrix_maps.h
changeset 2260 4274224f8a7d
child 2391 14a343be7a5a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lemon/concepts/matrix_maps.h	Tue Oct 24 17:19:16 2006 +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-2006
     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_MATRIX_MAPS_H
    1.23 +#define LEMON_CONCEPT_MATRIX_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 MatrixMap 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 matrix map concept
    1.40 +    template <typename K1, typename K2, typename V>
    1.41 +    class ReadMatrixMap
    1.42 +    {
    1.43 +    public:
    1.44 +      /// Map's first key type.
    1.45 +      typedef K1 FirstKey;    
    1.46 +      /// Map's second key type.
    1.47 +      typedef K2 SecondKey;    
    1.48 +      /// \brief Map's value type. 
    1.49 +      /// (The type of objects associated with the pairs of keys).
    1.50 +      typedef V Value;
    1.51 +
    1.52 +      // \bug Value don't need to be default constructible.
    1.53 +      /// Returns the value associated with a key.
    1.54 +      Value operator()(const FirstKey&, const SecondKey&) const {
    1.55 +	return Value();
    1.56 +      }
    1.57 +
    1.58 +      template <typename _ReadMatrixMap>
    1.59 +      struct Constraints {
    1.60 +
    1.61 +	void constraints() {
    1.62 +	  Value val = m(first_key, second_key);
    1.63 +	  val = m(first_key, second_key);
    1.64 +	  typename _ReadMatrixMap::Value own_val = 
    1.65 +	    m(own_first_key, own_second_key); 
    1.66 +	  own_val = m(own_first_key, own_second_key);
    1.67 +	  ignore_unused_variable_warning(val);
    1.68 +	  ignore_unused_variable_warning(own_val);
    1.69 +	}
    1.70 +
    1.71 +	FirstKey& first_key;
    1.72 +	SecondKey& second_key;	
    1.73 +	typename _ReadMatrixMap::FirstKey& own_first_key;
    1.74 +	typename _ReadMatrixMap::SecondKey& own_second_key;
    1.75 +	_ReadMatrixMap& m;
    1.76 +      };
    1.77 +      
    1.78 +    };
    1.79 +
    1.80 +
    1.81 +    /// Writable map concept
    1.82 +    template <typename K1, typename K2, typename V>
    1.83 +    class WriteMatrixMap {
    1.84 +    public:
    1.85 +      /// Map's first key type.
    1.86 +      typedef K1 FirstKey;    
    1.87 +      /// Map's second key type.
    1.88 +      typedef K2 SecondKey;    
    1.89 +      /// \brief Map's value type. 
    1.90 +      /// (The type of objects associated with the pairs of keys).
    1.91 +      typedef V Value;
    1.92 +
    1.93 +      /// Sets the value associated with the pair of keys.
    1.94 +      void set(const FirstKey&, const SecondKey& ,const Value&) {}
    1.95 +
    1.96 +      template <typename _WriteMatrixMap>
    1.97 +      struct Constraints {
    1.98 +	void constraints() {
    1.99 +	  // No constraints for constructor.
   1.100 +	  m.set(first_key, second_key, val);
   1.101 +	  m.set(own_first_key, own_second_key, own_val);
   1.102 +	}
   1.103 +
   1.104 +	Value& val;
   1.105 +	typename _WriteMatrixMap::Value own_val;
   1.106 +	FirstKey& first_key;
   1.107 +	SecondKey& second_key;
   1.108 +	typename _WriteMatrixMap::FirstKey& own_first_key;
   1.109 +	typename _WriteMatrixMap::SecondKey& own_second_key;
   1.110 +	_WriteMatrixMap& m;
   1.111 +
   1.112 +      };
   1.113 +    };
   1.114 +
   1.115 +    ///Read/Writable map concept
   1.116 +    template<typename K1, typename K2, typename V>
   1.117 +    class ReadWriteMatrixMap 
   1.118 +      : public ReadMatrixMap<K1, K2, V>, public WriteMatrixMap<K1, K2, V> {
   1.119 +    public:
   1.120 +      /// Map's first key type.
   1.121 +      typedef K1 FirstKey;    
   1.122 +      /// Map's second key type.
   1.123 +      typedef K2 SecondKey;    
   1.124 +      /// \brief Map's value type. 
   1.125 +      /// (The type of objects associated with the pairs of keys).
   1.126 +      typedef V Value;
   1.127 +
   1.128 +      /// Returns the value associated with a pair of keys.
   1.129 +      Value operator()(const FirstKey&, const SecondKey&) const { 
   1.130 +	return Value(); 
   1.131 +      }
   1.132 +      /// Sets the value associated with the pair of keys.
   1.133 +      void set(const FirstKey&, const SecondKey& ,const Value&) {}
   1.134 +
   1.135 +      template<typename _ReadWriteMatrixMap>
   1.136 +      struct Constraints {
   1.137 +	void constraints() {
   1.138 +	  checkConcept<ReadMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
   1.139 +	  checkConcept<WriteMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
   1.140 +	}
   1.141 +      };
   1.142 +    };
   1.143 +  
   1.144 +  
   1.145 +    ///Dereferable matrix map concept
   1.146 +    template<typename K1, typename K2, typename V, typename R, typename CR>
   1.147 +    class ReferenceMatrixMap : public ReadWriteMatrixMap<K1, K2, V>
   1.148 +    {
   1.149 +    public:
   1.150 +      /// Tag for reference maps.
   1.151 +      typedef True ReferenceMapTag;
   1.152 +      /// Map's first key type.
   1.153 +      typedef K1 FirstKey;    
   1.154 +      /// Map's second key type.
   1.155 +      typedef K1 SecondKey;    
   1.156 +      /// Map's value type. (The type of objects associated with the keys).
   1.157 +      typedef V Value;
   1.158 +      /// Map's reference type.
   1.159 +      typedef R Reference;
   1.160 +      /// Map's const reference type.
   1.161 +      typedef CR ConstReference;
   1.162 +
   1.163 +    protected:
   1.164 +      Value tmp;
   1.165 +    public:
   1.166 +
   1.167 +      ///Returns a reference to the value associated to a pair of keys.
   1.168 +      Reference operator()(const FirstKey&, const SecondKey&) { 
   1.169 +	return tmp; 
   1.170 +      }
   1.171 +      ///Returns a const reference to the value associated to a pair of keys.
   1.172 +      ConstReference operator()(const FirstKey&, const SecondKey&) const { 
   1.173 +	return tmp; 
   1.174 +      }
   1.175 +      /// Sets the value associated with the pair of keys.
   1.176 +      void set(const FirstKey&, const SecondKey& ,const Value&) {}
   1.177 +
   1.178 +      // \todo rethink this concept
   1.179 +      template<typename _ReferenceMatrixMap>
   1.180 +      struct ReferenceMapConcept {
   1.181 +
   1.182 +	void constraints() {
   1.183 +	  checkConcept<ReadWriteMatrixMap, _ReferenceMatrixMap >();
   1.184 +	  m(first_key, second_key) = val;
   1.185 +	  val  = m(first_key, second_key);
   1.186 +	  m(first_key, second_key) = ref;
   1.187 +	  ref = m(first_key, second_key);
   1.188 +	  m(own_first_key, own_second_key) = own_val;
   1.189 +	  own_val  = m(own_first_key, own_second_key);
   1.190 +	  m(own_first_key, own_second_key) = own_ref;
   1.191 +	  own_ref = m(own_first_key, own_second_key); 
   1.192 +	}
   1.193 +
   1.194 +	typename _ReferenceMatrixMap::Key& own_first_key;
   1.195 +	typename _ReferenceMatrixMap::Key& own_second_key;
   1.196 +	typename _ReferenceMatrixMap::Value& own_val;
   1.197 +	typename _ReferenceMatrixMap::Reference& own_ref;
   1.198 +	FirstKey& first_key;
   1.199 +	SecondKey& second_key;
   1.200 +	Value& val;
   1.201 +	Reference& ref;
   1.202 +	_ReferenceMatrixMap& m;
   1.203 +      };
   1.204 +    };
   1.205 +
   1.206 +    // @}
   1.207 +
   1.208 +  } //namespace concepts
   1.209 +} //namespace lemon
   1.210 +#endif // LEMON_CONCEPT_MATRIX_MAPS_H