lemon/concepts/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 29 Feb 2008 11:01:39 +0000
changeset 103 b68a7e348e00
parent 51 90201bb15a8d
child 79 d73c2e8b25cb
permissions -rw-r--r--
Port kruskal() and UnionFind from svn -r3468

The class type interface of Kruskal has not been ported yet
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 {
alpar@25
    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.
alpar@25
    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
alpar@25
    49
      /// Returns the value associated with a key.
alpar@25
    50
kpeter@48
    51
      /// Returns the value associated with a key.
kpeter@28
    52
      /// \bug Value shouldn't need to be default constructible.
alpar@25
    53
      ///
alpar@25
    54
      Value operator[](const Key &) const {return Value();}
alpar@25
    55
alpar@25
    56
      template<typename _ReadMap>
alpar@25
    57
      struct Constraints {
alpar@25
    58
	void constraints() {
alpar@25
    59
	  Value val = m[key];
alpar@25
    60
	  val = m[key];
alpar@25
    61
	  typename _ReadMap::Value own_val = m[own_key]; 
alpar@25
    62
	  own_val = m[own_key]; 
alpar@25
    63
alpar@25
    64
	  ignore_unused_variable_warning(val);
alpar@25
    65
	  ignore_unused_variable_warning(own_val);
alpar@25
    66
	  ignore_unused_variable_warning(key);
alpar@25
    67
	}
alpar@25
    68
	Key& key;
alpar@25
    69
	typename _ReadMap::Key& own_key;
alpar@25
    70
	_ReadMap& m;
alpar@25
    71
      };
alpar@25
    72
      
alpar@25
    73
    };
alpar@25
    74
alpar@25
    75
alpar@25
    76
    /// Writable map concept
kpeter@28
    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.
alpar@25
    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
alpar@25
    89
      /// Sets the value associated with a key.
alpar@25
    90
      void set(const Key &,const Value &) {}
alpar@25
    91
alpar@25
    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
	  // No constraints for constructor.
alpar@25
    99
	  m.set(key, val);
alpar@25
   100
	  m.set(own_key, own_val);
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
	}
alpar@25
   106
alpar@25
   107
	Value& val;
alpar@25
   108
	typename _WriteMap::Value own_val;
alpar@25
   109
	Key& key;
alpar@25
   110
	typename _WriteMap::Key& own_key;
alpar@25
   111
	_WriteMap& m;
alpar@25
   112
alpar@25
   113
      };
alpar@25
   114
    };
alpar@25
   115
kpeter@48
   116
    /// Read/writable map concept
kpeter@28
   117
    
kpeter@28
   118
    /// Read/writable map concept.
kpeter@28
   119
    ///
alpar@25
   120
    template<typename K, typename T>
alpar@25
   121
    class ReadWriteMap : public ReadMap<K,T>,
kpeter@35
   122
			 public WriteMap<K,T>
alpar@25
   123
    {
alpar@25
   124
    public:
kpeter@35
   125
      /// The key type of the map.
alpar@25
   126
      typedef K Key;    
kpeter@35
   127
      /// The value type of the map. (The type of objects associated with the keys).
alpar@25
   128
      typedef T Value;
alpar@25
   129
alpar@25
   130
      /// Returns the value associated with a key.
alpar@25
   131
      Value operator[](const Key &) const {return Value();}
alpar@25
   132
      /// Sets the value associated with a key.
alpar@25
   133
      void set(const Key & ,const Value &) {}
alpar@25
   134
alpar@25
   135
      template<typename _ReadWriteMap>
alpar@25
   136
      struct Constraints {
alpar@25
   137
	void constraints() {
alpar@25
   138
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
alpar@25
   139
	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
alpar@25
   140
	}
alpar@25
   141
      };
alpar@25
   142
    };
alpar@25
   143
  
alpar@25
   144
  
kpeter@28
   145
    /// Dereferable map concept
kpeter@28
   146
    
kpeter@28
   147
    /// Dereferable map concept.
kpeter@28
   148
    ///
kpeter@48
   149
    /// \todo Rethink this concept.
alpar@25
   150
    template<typename K, typename T, typename R, typename CR>
alpar@25
   151
    class ReferenceMap : public ReadWriteMap<K,T>
alpar@25
   152
    {
alpar@25
   153
    public:
alpar@25
   154
      /// Tag for reference maps.
alpar@25
   155
      typedef True ReferenceMapTag;
kpeter@35
   156
      /// The key type of the map.
alpar@25
   157
      typedef K Key;    
kpeter@35
   158
      /// The value type of the map. (The type of objects associated with the keys).
alpar@25
   159
      typedef T Value;
kpeter@35
   160
      /// The reference type of the map.
alpar@25
   161
      typedef R Reference;
kpeter@35
   162
      /// The const reference type of the map.
alpar@25
   163
      typedef CR ConstReference;
alpar@25
   164
alpar@25
   165
    protected:
alpar@25
   166
      Value tmp;
alpar@25
   167
    public:
alpar@25
   168
kpeter@48
   169
      ///Returns a reference to the value associated with a key.
alpar@25
   170
      Reference operator[](const Key &) { return tmp; }
kpeter@48
   171
      ///Returns a const reference to the value associated with a key.
kpeter@28
   172
      ConstReference operator[](const Key &) const { return tmp; }
alpar@25
   173
      /// Sets the value associated with a 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 >();
alpar@25
   180
	  m[key] = val;
alpar@25
   181
	  val  = m[key];
alpar@25
   182
	  m[key] = ref;
alpar@25
   183
	  ref = m[key];
alpar@25
   184
	  m[own_key] = own_val;
alpar@25
   185
	  own_val  = m[own_key];
alpar@25
   186
	  m[own_key] = own_ref;
alpar@25
   187
	  own_ref = m[own_key];	  	  
alpar@25
   188
	}
alpar@25
   189
alpar@25
   190
	typename _ReferenceMap::Key& own_key;
alpar@25
   191
	typename _ReferenceMap::Value& own_val;
kpeter@74
   192
	typename _ReferenceMap::Reference own_ref;
alpar@25
   193
	Key& key;
alpar@25
   194
	Value& val;
kpeter@74
   195
	Reference ref;
alpar@25
   196
	_ReferenceMap& m;
alpar@25
   197
      };
alpar@25
   198
    };
alpar@25
   199
alpar@25
   200
    // @}
alpar@25
   201
alpar@25
   202
  } //namespace concepts
kpeter@28
   203
alpar@25
   204
} //namespace lemon
kpeter@28
   205
alpar@25
   206
#endif // LEMON_CONCEPT_MAPS_H