lemon/concepts/matrix_maps.h
author deba
Tue, 30 Oct 2007 10:51:07 +0000
changeset 2504 46a82ce84cc6
parent 2260 4274224f8a7d
child 2553 bfced05fa852
permissions -rw-r--r--
Bug fix
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     9  * Permission to use, modify and distribute this software is granted
    10  * provided that this copyright notice appears in all copies. For
    11  * precise terms see the accompanying LICENSE file.
    12  *
    13  * This software is provided "AS IS" with no warranty of any kind,
    14  * express or implied, and with no claim as to its suitability for any
    15  * purpose.
    16  *
    17  */
    18 
    19 #ifndef LEMON_CONCEPT_MATRIX_MAPS_H
    20 #define LEMON_CONCEPT_MATRIX_MAPS_H
    21 
    22 #include <lemon/bits/utility.h>
    23 #include <lemon/concept_check.h>
    24 
    25 ///\ingroup concept
    26 ///\file
    27 ///\brief MatrixMap concepts checking classes for testing and documenting.
    28 
    29 namespace lemon {
    30 
    31   namespace concepts {
    32   
    33     /// \addtogroup concept
    34     /// @{
    35 
    36     /// Readable matrix map concept
    37     template <typename K1, typename K2, typename V>
    38     class ReadMatrixMap
    39     {
    40     public:
    41       /// Map's first key type.
    42       typedef K1 FirstKey;    
    43       /// Map's second key type.
    44       typedef K2 SecondKey;    
    45       /// \brief Map's value type. 
    46       /// (The type of objects associated with the pairs of keys).
    47       typedef V Value;
    48 
    49       // \bug Value don't need to be default constructible.
    50       /// Returns the value associated with a key.
    51       Value operator()(const FirstKey&, const SecondKey&) const {
    52 	return Value();
    53       }
    54 
    55       template <typename _ReadMatrixMap>
    56       struct Constraints {
    57 
    58 	void constraints() {
    59 	  Value val = m(first_key, second_key);
    60 	  val = m(first_key, second_key);
    61 	  typename _ReadMatrixMap::Value own_val = 
    62 	    m(own_first_key, own_second_key); 
    63 	  own_val = m(own_first_key, own_second_key);
    64 	  ignore_unused_variable_warning(val);
    65 	  ignore_unused_variable_warning(own_val);
    66 	}
    67 
    68 	FirstKey& first_key;
    69 	SecondKey& second_key;	
    70 	typename _ReadMatrixMap::FirstKey& own_first_key;
    71 	typename _ReadMatrixMap::SecondKey& own_second_key;
    72 	_ReadMatrixMap& m;
    73       };
    74       
    75     };
    76 
    77 
    78     /// Writable map concept
    79     template <typename K1, typename K2, typename V>
    80     class WriteMatrixMap {
    81     public:
    82       /// Map's first key type.
    83       typedef K1 FirstKey;    
    84       /// Map's second key type.
    85       typedef K2 SecondKey;    
    86       /// \brief Map's value type. 
    87       /// (The type of objects associated with the pairs of keys).
    88       typedef V Value;
    89 
    90       /// Sets the value associated with the pair of keys.
    91       void set(const FirstKey&, const SecondKey& ,const Value&) {}
    92 
    93       template <typename _WriteMatrixMap>
    94       struct Constraints {
    95 	void constraints() {
    96 	  // No constraints for constructor.
    97 	  m.set(first_key, second_key, val);
    98 	  m.set(own_first_key, own_second_key, own_val);
    99 	}
   100 
   101 	Value& val;
   102 	typename _WriteMatrixMap::Value own_val;
   103 	FirstKey& first_key;
   104 	SecondKey& second_key;
   105 	typename _WriteMatrixMap::FirstKey& own_first_key;
   106 	typename _WriteMatrixMap::SecondKey& own_second_key;
   107 	_WriteMatrixMap& m;
   108 
   109       };
   110     };
   111 
   112     ///Read/Writable map concept
   113     template<typename K1, typename K2, typename V>
   114     class ReadWriteMatrixMap 
   115       : public ReadMatrixMap<K1, K2, V>, public WriteMatrixMap<K1, K2, V> {
   116     public:
   117       /// Map's first key type.
   118       typedef K1 FirstKey;    
   119       /// Map's second key type.
   120       typedef K2 SecondKey;    
   121       /// \brief Map's value type. 
   122       /// (The type of objects associated with the pairs of keys).
   123       typedef V Value;
   124 
   125       /// Returns the value associated with a pair of keys.
   126       Value operator()(const FirstKey&, const SecondKey&) const { 
   127 	return Value(); 
   128       }
   129       /// Sets the value associated with the pair of keys.
   130       void set(const FirstKey&, const SecondKey& ,const Value&) {}
   131 
   132       template<typename _ReadWriteMatrixMap>
   133       struct Constraints {
   134 	void constraints() {
   135 	  checkConcept<ReadMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
   136 	  checkConcept<WriteMatrixMap<K1, K2, V>, _ReadWriteMatrixMap >();
   137 	}
   138       };
   139     };
   140   
   141   
   142     ///Dereferable matrix map concept
   143     template<typename K1, typename K2, typename V, typename R, typename CR>
   144     class ReferenceMatrixMap : public ReadWriteMatrixMap<K1, K2, V>
   145     {
   146     public:
   147       /// Tag for reference maps.
   148       typedef True ReferenceMapTag;
   149       /// Map's first key type.
   150       typedef K1 FirstKey;    
   151       /// Map's second key type.
   152       typedef K1 SecondKey;    
   153       /// Map's value type. (The type of objects associated with the keys).
   154       typedef V Value;
   155       /// Map's reference type.
   156       typedef R Reference;
   157       /// Map's const reference type.
   158       typedef CR ConstReference;
   159 
   160     protected:
   161       Value tmp;
   162     public:
   163 
   164       ///Returns a reference to the value associated to a pair of keys.
   165       Reference operator()(const FirstKey&, const SecondKey&) { 
   166 	return tmp; 
   167       }
   168       ///Returns a const reference to the value associated to a pair of keys.
   169       ConstReference operator()(const FirstKey&, const SecondKey&) const { 
   170 	return tmp; 
   171       }
   172       /// Sets the value associated with the pair of keys.
   173       void set(const FirstKey&, const SecondKey& ,const Value&) {}
   174 
   175       // \todo rethink this concept
   176       template<typename _ReferenceMatrixMap>
   177       struct ReferenceMapConcept {
   178 
   179 	void constraints() {
   180 	  checkConcept<ReadWriteMatrixMap, _ReferenceMatrixMap >();
   181 	  m(first_key, second_key) = val;
   182 	  val  = m(first_key, second_key);
   183 	  m(first_key, second_key) = ref;
   184 	  ref = m(first_key, second_key);
   185 	  m(own_first_key, own_second_key) = own_val;
   186 	  own_val  = m(own_first_key, own_second_key);
   187 	  m(own_first_key, own_second_key) = own_ref;
   188 	  own_ref = m(own_first_key, own_second_key); 
   189 	}
   190 
   191 	typename _ReferenceMatrixMap::Key& own_first_key;
   192 	typename _ReferenceMatrixMap::Key& own_second_key;
   193 	typename _ReferenceMatrixMap::Value& own_val;
   194 	typename _ReferenceMatrixMap::Reference& own_ref;
   195 	FirstKey& first_key;
   196 	SecondKey& second_key;
   197 	Value& val;
   198 	Reference& ref;
   199 	_ReferenceMatrixMap& m;
   200       };
   201     };
   202 
   203     // @}
   204 
   205   } //namespace concepts
   206 } //namespace lemon
   207 #endif // LEMON_CONCEPT_MATRIX_MAPS_H