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