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