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