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