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