src/lemon/concept/maps.h
author deba
Wed, 15 Dec 2004 19:56:55 +0000
changeset 1037 3eaff8d04171
parent 987 87f7c54892df
child 1042 9c5daf8e3e32
permissions -rw-r--r--
graph_io under construction
This is a working version, but needs more improvments.

todo:
documention + fix the file format
improve the exception system

add some possible asserts

tutorials
klao@959
     1
/* -*- C++ -*-
klao@959
     2
 * src/lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
klao@959
     3
 *
klao@959
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
klao@959
     5
 * (Egervary Combinatorial Optimization Research Group, 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
klao@959
    20
#include <lemon/concept_check.h>
klao@959
    21
klao@959
    22
///\ingroup concept
klao@959
    23
///\file
klao@959
    24
///\brief Map concepts checking classes for testing and documenting.
klao@959
    25
klao@959
    26
namespace lemon {
klao@959
    27
klao@959
    28
  namespace concept {
klao@959
    29
  
klao@959
    30
    /// \addtogroup concept
klao@959
    31
    /// @{
klao@959
    32
klao@959
    33
    /// Readable map concept
klao@959
    34
    template<typename K, typename T>
klao@959
    35
    class ReadMap
klao@959
    36
    {
klao@959
    37
    public:
klao@959
    38
      /// Map's key type.
alpar@987
    39
      typedef K Key;    
klao@959
    40
      /// Map's value type. (The type of objects associated with the keys).
alpar@987
    41
      typedef T Value;
klao@959
    42
deba@989
    43
      // \bug Value don't need to be default constructible.
klao@959
    44
      /// Returns the value associated with a key.
deba@989
    45
      Value operator[](const Key &) const {return Value();}
klao@959
    46
deba@989
    47
      template<typename _ReadMap>
deba@989
    48
      struct Constraints {
deba@989
    49
deba@989
    50
	void constraints() {
deba@989
    51
	  Value val = m[key];
deba@989
    52
	  val = m[key];
deba@989
    53
	  typename _ReadMap::Value own_val = m[own_key]; 
deba@989
    54
	  own_val = m[own_key]; 
deba@989
    55
deba@989
    56
	  ignore_unused_variable_warning(val);
deba@989
    57
	  ignore_unused_variable_warning(own_val);
deba@989
    58
	  ignore_unused_variable_warning(key);
deba@989
    59
	}
deba@989
    60
	Key& key;
deba@989
    61
	typename _ReadMap::Key& own_key;
deba@989
    62
	_ReadMap& m;
deba@989
    63
      };
deba@989
    64
      
klao@959
    65
    };
klao@959
    66
klao@959
    67
klao@959
    68
    /// Writable map concept
klao@959
    69
    template<typename K, typename T>
klao@959
    70
    class WriteMap
klao@959
    71
    {
klao@959
    72
    public:
klao@959
    73
      /// Map's key type.
alpar@987
    74
      typedef K Key;    
klao@959
    75
      /// Map's value type. (The type of objects associated with the keys).
alpar@987
    76
      typedef T Value;
klao@959
    77
klao@959
    78
      /// Sets the value associated with a key.
alpar@987
    79
      void set(const Key &k,const Value &t) {}
klao@959
    80
klao@959
    81
      ///Default constructor
klao@959
    82
      WriteMap() {}
deba@989
    83
deba@989
    84
      template <typename _WriteMap>
deba@989
    85
      struct Constraints {
deba@989
    86
	void constraints() {
deba@989
    87
	  // No constraints for constructor.
deba@989
    88
	  m.set(key, val);
deba@989
    89
	  m.set(own_key, own_val);
deba@989
    90
	  ignore_unused_variable(key);
deba@989
    91
	  ignore_unused_variable(val);
deba@989
    92
	  ignore_unused_variable(own_key);
deba@989
    93
	  ignore_unused_variable(own_val);
deba@989
    94
	}
deba@989
    95
deba@989
    96
	Value& val;
deba@989
    97
	typename _WriteMap::Value own_val;
deba@989
    98
	Key& key;
deba@989
    99
	typename _WriteMap::Key& own_key;
deba@989
   100
	WriteMap& m;
deba@989
   101
deba@989
   102
      };
klao@959
   103
    };
klao@959
   104
klao@959
   105
    ///Read/Writable map concept
klao@959
   106
    template<typename K, typename T>
klao@959
   107
    class ReadWriteMap : public ReadMap<K,T>,
klao@959
   108
			    public WriteMap<K,T>
klao@959
   109
    {
klao@959
   110
    public:
klao@959
   111
      /// Map's key type.
alpar@987
   112
      typedef K Key;    
klao@959
   113
      /// Map's value type. (The type of objects associated with the keys).
alpar@987
   114
      typedef T Value;
klao@959
   115
klao@959
   116
      /// Returns the value associated with a key.
alpar@987
   117
      Value operator[](const Key &k) const {return Value();}
klao@959
   118
      /// Sets the value associated with a key.
alpar@987
   119
      void set(const Key &k,const Value &t) {}
klao@959
   120
deba@989
   121
      template<typename _ReadWriteMap>
deba@989
   122
      struct Constraints {
deba@989
   123
	void constraints() {
deba@989
   124
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
deba@989
   125
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
deba@989
   126
	}
deba@989
   127
      };
klao@959
   128
    };
klao@959
   129
  
klao@959
   130
  
klao@959
   131
    ///Dereferable map concept
deba@989
   132
    template<typename K, typename T, typename R, typename CR>
klao@959
   133
    class ReferenceMap : public ReadWriteMap<K,T>
klao@959
   134
    {
klao@959
   135
    public:
klao@959
   136
      /// Map's key type.
alpar@987
   137
      typedef K Key;    
klao@959
   138
      /// Map's value type. (The type of objects associated with the keys).
alpar@987
   139
      typedef T Value;
deba@989
   140
      /// Map's reference type.
deba@989
   141
      typedef R Reference;
deba@989
   142
      /// Map's const reference type.
deba@989
   143
      typedef CR ConstReference;
klao@959
   144
klao@959
   145
    protected:
alpar@987
   146
      Value tmp;
klao@959
   147
    public:
klao@959
   148
klao@959
   149
      ///Returns a reference to the value associated to a key.
alpar@987
   150
      Reference operator[](const Key &i) { return tmp; }
klao@959
   151
      ///Returns a const reference to the value associated to a key.
alpar@987
   152
      ConstReference operator[](const Key &i) const
klao@959
   153
      { return tmp; }
klao@959
   154
      /// Sets the value associated with a key.
alpar@987
   155
      void set(const Key &k,const Value &t) { operator[](k)=t; }
klao@959
   156
deba@989
   157
      // \todo rethink this concept
deba@989
   158
      template<typename _ReferenceMap>
deba@989
   159
      struct ReferenceMapConcept {
deba@989
   160
deba@989
   161
	void constraints() {
deba@989
   162
	  checkConcept<ReadWriteMap, _ReferenceMap >();
deba@989
   163
	  m[key] = val;
deba@989
   164
	  val  = m[key];
deba@989
   165
	  m[key] = ref;
deba@989
   166
	  ref = m[key];
deba@989
   167
	  m[own_key] = own_val;
deba@989
   168
	  own_val  = m[own_key];
deba@989
   169
	  m[own_key] = own_ref;
deba@989
   170
	  own_ref = m[own_key];	  	  
deba@989
   171
	}
deba@989
   172
deba@989
   173
	typename _ReferenceMap::Key& own_key;
deba@989
   174
	typename _ReferenceMap::Value& own_val;
deba@989
   175
	typename _ReferenceMap::Reference& own_ref;
deba@989
   176
	Key& key;
deba@989
   177
	Value& val;
deba@989
   178
	Reference& ref;
deba@989
   179
	ReferenceMap& m;
deba@989
   180
      };
klao@959
   181
    };
klao@959
   182
klao@959
   183
    // @}
klao@959
   184
klao@959
   185
  } //namespace concept
klao@959
   186
} //namespace lemon
klao@959
   187
#endif // LEMON_CONCEPT_MAPS_H