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