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