lemon/concepts/maps.h
author kpeter
Tue, 05 Feb 2008 12:41:05 +0000
changeset 2564 3250756f5add
parent 2553 bfced05fa852
permissions -rw-r--r--
Several doc improvements and fixes in maps.h and concepts/maps.h.
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@2553
     5
 * Copyright (C) 2003-2008
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
kpeter@2564
    37
kpeter@2564
    38
    /// Readable map concept.
kpeter@2564
    39
    ///
alpar@2260
    40
    template<typename K, typename T>
alpar@2260
    41
    class ReadMap
alpar@2260
    42
    {
alpar@2260
    43
    public:
kpeter@2564
    44
      /// The key type of the map.
alpar@2260
    45
      typedef K Key;    
kpeter@2564
    46
      /// The value type of the map. (The type of objects associated with the keys).
alpar@2260
    47
      typedef T Value;
alpar@2260
    48
alpar@2260
    49
      /// Returns the value associated with a key.
kpeter@2564
    50
kpeter@2564
    51
      /// Returns the value associated with a key.
kpeter@2564
    52
      /// \bug Value shouldn't need to be default constructible.
kpeter@2564
    53
      ///
alpar@2260
    54
      Value operator[](const Key &) const {return Value();}
alpar@2260
    55
alpar@2260
    56
      template<typename _ReadMap>
alpar@2260
    57
      struct Constraints {
alpar@2260
    58
alpar@2260
    59
	void constraints() {
alpar@2260
    60
	  Value val = m[key];
alpar@2260
    61
	  val = m[key];
alpar@2260
    62
	  typename _ReadMap::Value own_val = m[own_key]; 
alpar@2260
    63
	  own_val = m[own_key]; 
alpar@2260
    64
alpar@2260
    65
	  ignore_unused_variable_warning(val);
alpar@2260
    66
	  ignore_unused_variable_warning(own_val);
alpar@2260
    67
	  ignore_unused_variable_warning(key);
alpar@2260
    68
	}
alpar@2260
    69
	Key& key;
alpar@2260
    70
	typename _ReadMap::Key& own_key;
alpar@2260
    71
	_ReadMap& m;
alpar@2260
    72
      };
alpar@2260
    73
      
alpar@2260
    74
    };
alpar@2260
    75
alpar@2260
    76
alpar@2260
    77
    /// Writable map concept
kpeter@2564
    78
    
kpeter@2564
    79
    /// Writable map concept.
kpeter@2564
    80
    ///
alpar@2260
    81
    template<typename K, typename T>
alpar@2260
    82
    class WriteMap
alpar@2260
    83
    {
alpar@2260
    84
    public:
kpeter@2564
    85
      /// The key type of the map.
alpar@2260
    86
      typedef K Key;    
kpeter@2564
    87
      /// The value type of the map. (The type of objects associated with the keys).
alpar@2260
    88
      typedef T Value;
alpar@2260
    89
alpar@2260
    90
      /// Sets the value associated with a key.
alpar@2260
    91
      void set(const Key &,const Value &) {}
alpar@2260
    92
alpar@2260
    93
      ///Default constructor
alpar@2260
    94
      WriteMap() {}
alpar@2260
    95
alpar@2260
    96
      template <typename _WriteMap>
alpar@2260
    97
      struct Constraints {
alpar@2260
    98
	void constraints() {
alpar@2260
    99
	  // No constraints for constructor.
alpar@2260
   100
	  m.set(key, val);
alpar@2260
   101
	  m.set(own_key, own_val);
alpar@2260
   102
	  ignore_unused_variable_warning(key);
alpar@2260
   103
	  ignore_unused_variable_warning(val);
alpar@2260
   104
	  ignore_unused_variable_warning(own_key);
alpar@2260
   105
	  ignore_unused_variable_warning(own_val);
alpar@2260
   106
	}
alpar@2260
   107
alpar@2260
   108
	Value& val;
alpar@2260
   109
	typename _WriteMap::Value own_val;
alpar@2260
   110
	Key& key;
alpar@2260
   111
	typename _WriteMap::Key& own_key;
alpar@2260
   112
	_WriteMap& m;
alpar@2260
   113
alpar@2260
   114
      };
alpar@2260
   115
    };
alpar@2260
   116
kpeter@2564
   117
    /// Read/writable map concept
kpeter@2564
   118
    
kpeter@2564
   119
    /// Read/writable map concept.
kpeter@2564
   120
    ///
alpar@2260
   121
    template<typename K, typename T>
alpar@2260
   122
    class ReadWriteMap : public ReadMap<K,T>,
kpeter@2564
   123
			 public WriteMap<K,T>
alpar@2260
   124
    {
alpar@2260
   125
    public:
kpeter@2564
   126
      /// The key type of the map.
alpar@2260
   127
      typedef K Key;    
kpeter@2564
   128
      /// The value type of the map. (The type of objects associated with the keys).
alpar@2260
   129
      typedef T Value;
alpar@2260
   130
alpar@2260
   131
      /// Returns the value associated with a key.
alpar@2260
   132
      Value operator[](const Key &) const {return Value();}
alpar@2260
   133
      /// Sets the value associated with a key.
alpar@2260
   134
      void set(const Key & ,const Value &) {}
alpar@2260
   135
alpar@2260
   136
      template<typename _ReadWriteMap>
alpar@2260
   137
      struct Constraints {
alpar@2260
   138
	void constraints() {
alpar@2260
   139
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
alpar@2260
   140
	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
alpar@2260
   141
	}
alpar@2260
   142
      };
alpar@2260
   143
    };
alpar@2260
   144
  
alpar@2260
   145
  
alpar@2260
   146
    ///Dereferable map concept
kpeter@2564
   147
    
kpeter@2564
   148
    /// Dereferable map concept.
kpeter@2564
   149
    ///
kpeter@2564
   150
    /// \todo Rethink this concept.
alpar@2260
   151
    template<typename K, typename T, typename R, typename CR>
alpar@2260
   152
    class ReferenceMap : public ReadWriteMap<K,T>
alpar@2260
   153
    {
alpar@2260
   154
    public:
alpar@2260
   155
      /// Tag for reference maps.
alpar@2260
   156
      typedef True ReferenceMapTag;
kpeter@2564
   157
      /// The key type of the map.
alpar@2260
   158
      typedef K Key;    
kpeter@2564
   159
      /// The value type of the map. (The type of objects associated with the keys).
alpar@2260
   160
      typedef T Value;
kpeter@2564
   161
      /// The reference type of the map.
alpar@2260
   162
      typedef R Reference;
kpeter@2564
   163
      /// The const reference type of the map.
alpar@2260
   164
      typedef CR ConstReference;
alpar@2260
   165
alpar@2260
   166
    protected:
alpar@2260
   167
      Value tmp;
alpar@2260
   168
    public:
alpar@2260
   169
kpeter@2564
   170
      ///Returns a reference to the value associated with a key.
alpar@2260
   171
      Reference operator[](const Key &) { return tmp; }
kpeter@2564
   172
      ///Returns a const reference to the value associated with a key.
kpeter@2564
   173
      ConstReference operator[](const Key &) const { return tmp; }
alpar@2260
   174
      /// Sets the value associated with a key.
alpar@2260
   175
      void set(const Key &k,const Value &t) { operator[](k)=t; }
alpar@2260
   176
alpar@2260
   177
      template<typename _ReferenceMap>
kpeter@2564
   178
      struct Constraints {
alpar@2260
   179
alpar@2260
   180
	void constraints() {
kpeter@2564
   181
	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
alpar@2260
   182
	  m[key] = val;
alpar@2260
   183
	  val  = m[key];
alpar@2260
   184
	  m[key] = ref;
alpar@2260
   185
	  ref = m[key];
alpar@2260
   186
	  m[own_key] = own_val;
alpar@2260
   187
	  own_val  = m[own_key];
alpar@2260
   188
	  m[own_key] = own_ref;
alpar@2260
   189
	  own_ref = m[own_key];	  	  
alpar@2260
   190
	}
alpar@2260
   191
alpar@2260
   192
	typename _ReferenceMap::Key& own_key;
alpar@2260
   193
	typename _ReferenceMap::Value& own_val;
kpeter@2564
   194
	typename _ReferenceMap::Reference own_ref;
alpar@2260
   195
	Key& key;
alpar@2260
   196
	Value& val;
kpeter@2564
   197
	Reference ref;
alpar@2260
   198
	_ReferenceMap& m;
alpar@2260
   199
      };
alpar@2260
   200
    };
alpar@2260
   201
alpar@2260
   202
    // @}
alpar@2260
   203
alpar@2260
   204
  } //namespace concepts
kpeter@2564
   205
alpar@2260
   206
} //namespace lemon
kpeter@2564
   207
alpar@2260
   208
#endif // LEMON_CONCEPT_MAPS_H