src/lemon/concept/maps.h
author klao
Mon, 08 Nov 2004 16:42:35 +0000
changeset 971 643d3192ebc8
child 987 87f7c54892df
permissions -rw-r--r--
remove an annoying warning
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.
klao@959
    39
      typedef K KeyType;    
klao@959
    40
      /// Map's value type. (The type of objects associated with the keys).
klao@959
    41
      typedef T ValueType;
klao@959
    42
klao@959
    43
      /// Returns the value associated with a key.
klao@959
    44
      ValueType operator[](const KeyType &k) const {return ValueType();}
klao@959
    45
klao@959
    46
      ///Default constructor
klao@959
    47
      ReadMap() {}
klao@959
    48
    };
klao@959
    49
klao@959
    50
klao@959
    51
    /// Writable map concept
klao@959
    52
    template<typename K, typename T>
klao@959
    53
    class WriteMap
klao@959
    54
    {
klao@959
    55
    public:
klao@959
    56
      /// Map's key type.
klao@959
    57
      typedef K KeyType;    
klao@959
    58
      /// Map's value type. (The type of objects associated with the keys).
klao@959
    59
      typedef T ValueType;
klao@959
    60
klao@959
    61
      /// Sets the value associated with a key.
klao@959
    62
      void set(const KeyType &k,const ValueType &t) {}
klao@959
    63
klao@959
    64
      ///Default constructor
klao@959
    65
      WriteMap() {}
klao@959
    66
    };
klao@959
    67
klao@959
    68
    ///Read/Writable map concept
klao@959
    69
    template<typename K, typename T>
klao@959
    70
    class ReadWriteMap : public ReadMap<K,T>,
klao@959
    71
			    public WriteMap<K,T>
klao@959
    72
    {
klao@959
    73
    public:
klao@959
    74
      /// Map's key type.
klao@959
    75
      typedef K KeyType;    
klao@959
    76
      /// Map's value type. (The type of objects associated with the keys).
klao@959
    77
      typedef T ValueType;
klao@959
    78
klao@959
    79
      /// Returns the value associated with a key.
klao@959
    80
      ValueType operator[](const KeyType &k) const {return ValueType();}
klao@959
    81
      /// Sets the value associated with a key.
klao@959
    82
      void set(const KeyType &k,const ValueType &t) {}
klao@959
    83
klao@959
    84
      ///Default constructor
klao@959
    85
      ReadWriteMap() {}
klao@959
    86
    };
klao@959
    87
  
klao@959
    88
  
klao@959
    89
    ///Dereferable map concept
klao@959
    90
    template<typename K, typename T>
klao@959
    91
    class ReferenceMap : public ReadWriteMap<K,T>
klao@959
    92
    {
klao@959
    93
    public:
klao@959
    94
      /// Map's key type.
klao@959
    95
      typedef K KeyType;    
klao@959
    96
      /// Map's value type. (The type of objects associated with the keys).
klao@959
    97
      typedef T ValueType;
klao@959
    98
klao@959
    99
    protected:
klao@959
   100
      ValueType tmp;
klao@959
   101
    public:
klao@959
   102
      typedef ValueType& ReferenceType;
klao@959
   103
      /// Map's const reference type.
klao@959
   104
      typedef const ValueType& ConstReferenceType;
klao@959
   105
klao@959
   106
      ///Returns a reference to the value associated to a key.
klao@959
   107
      ReferenceType operator[](const KeyType &i) { return tmp; }
klao@959
   108
      ///Returns a const reference to the value associated to a key.
klao@959
   109
      ConstReferenceType operator[](const KeyType &i) const
klao@959
   110
      { return tmp; }
klao@959
   111
      /// Sets the value associated with a key.
klao@959
   112
      void set(const KeyType &k,const ValueType &t) { operator[](k)=t; }
klao@959
   113
klao@959
   114
      ///Default constructor
klao@959
   115
      ReferenceMap() {}
klao@959
   116
    };
klao@959
   117
klao@959
   118
klao@959
   119
    template<typename Item, typename T, typename Graph>
klao@959
   120
    class GraphMap : public ReadWriteMap<Item, T> {
klao@959
   121
      // I really, really don't like the idea that every graph should have
klao@959
   122
      // reference maps! --klao
klao@959
   123
klao@959
   124
    private:
klao@959
   125
      // We state explicitly that graph maps have no default constructor?
klao@959
   126
      GraphMap();
klao@959
   127
klao@959
   128
    public:
klao@959
   129
      explicit GraphMap(Graph const&) {}
klao@959
   130
      // value for initializing
klao@959
   131
      GraphMap(Graph const&, T) {}
klao@959
   132
klao@959
   133
      // this probably should be required:
klao@959
   134
      GraphMap(GraphMap const&) {}
klao@959
   135
      GraphMap& operator=(GraphMap const&) { return *this; }
klao@959
   136
klao@959
   137
      // but this is a absolute no-op! We should provide a more generic
klao@959
   138
      // graph-map-copy operation.
klao@959
   139
      //
klao@959
   140
      // template<typename TT>
klao@959
   141
      // GraphMap(GraphMap<TT> const&);
klao@959
   142
      //
klao@959
   143
      // template<typename TT>
klao@959
   144
      // GraphMap& operator=(const GraphMap<TT>&);
klao@959
   145
    };
klao@959
   146
klao@959
   147
klao@959
   148
    /****************  Concept-checking classes  ****************/
klao@959
   149
klao@959
   150
    template<typename ReadMap>
klao@959
   151
    struct ReadMapConcept {
klao@959
   152
      typedef typename ReadMap::KeyType KeyType;
klao@959
   153
      typedef typename ReadMap::ValueType ValueType;
klao@959
   154
klao@959
   155
      void constraints() {
klao@959
   156
	// No constraints for constructor.
klao@959
   157
klao@959
   158
	// What are the requirement for the ValueType?
klao@959
   159
	// CopyConstructible? Assignable? None of these?
klao@959
   160
	ValueType v = m[k];
klao@959
   161
	v = m[k];
klao@959
   162
klao@959
   163
	// FIXME:
klao@959
   164
	ignore_unused_variable_warning(v);
klao@959
   165
      }
klao@959
   166
klao@959
   167
      ReadMap m;
klao@959
   168
      KeyType k;
klao@959
   169
    };
klao@959
   170
klao@959
   171
    template<typename WriteMap>
klao@959
   172
    struct WriteMapConcept {
klao@959
   173
      typedef typename WriteMap::KeyType KeyType;
klao@959
   174
      typedef typename WriteMap::ValueType ValueType;
klao@959
   175
klao@959
   176
      void constraints() {
klao@959
   177
	// No constraints for constructor.
klao@959
   178
klao@959
   179
	m.set(k, v);
klao@959
   180
      }
klao@959
   181
klao@959
   182
      WriteMap m;
klao@959
   183
      KeyType k;
klao@959
   184
      ValueType v;
klao@959
   185
    };
klao@959
   186
klao@959
   187
    template<typename ReadWriteMap>
klao@959
   188
    struct ReadWriteMapConcept {
klao@959
   189
      void constraints() {
klao@959
   190
	function_requires< ReadMapConcept<ReadWriteMap> >();
klao@959
   191
	function_requires< WriteMapConcept<ReadWriteMap> >();
klao@959
   192
      }
klao@959
   193
    };
klao@959
   194
klao@959
   195
    template<typename ReferenceMap>
klao@959
   196
    struct ReferenceMapConcept {
klao@959
   197
      typedef typename ReferenceMap::KeyType KeyType;
klao@959
   198
      typedef typename ReferenceMap::ValueType ValueType;
klao@959
   199
      typedef typename ReferenceMap::ReferenceType ReferenceType;
klao@959
   200
klao@959
   201
      // What for is this?
klao@959
   202
      typedef typename ReferenceMap::ConstReferenceType ConstReferenceType;
klao@959
   203
klao@959
   204
      void constraints() {
klao@959
   205
	function_requires< ReadWriteMapConcept<ReferenceMap> >();
klao@959
   206
klao@959
   207
	m[k] = v;
klao@959
   208
	// Or should we require real reference?
klao@959
   209
	// Like this:
klao@959
   210
	// ValueType &vv = m[k];
klao@959
   211
	// ignore_unused_variable_warning(vv);
klao@959
   212
      }
klao@959
   213
klao@959
   214
      ReferenceMap m;
klao@959
   215
      KeyType k;
klao@959
   216
      ValueType v;
klao@959
   217
    };
klao@959
   218
klao@959
   219
    /// \todo GraphMapConceptCheck
klao@959
   220
klao@959
   221
    template<typename GraphMap, typename Graph>
klao@959
   222
    struct GraphMapConcept {
klao@959
   223
      void constraints() {
klao@959
   224
	function_requires< ReadWriteMapConcept<GraphMap> >();
klao@959
   225
	// Construction with a graph parameter
klao@959
   226
	GraphMap a(g);
klao@959
   227
	// Ctor with a graph and a default value parameter
klao@959
   228
	GraphMap a2(g,t);
klao@959
   229
	// Copy ctor. Do we need it?
klao@959
   230
	GraphMap b=c;
klao@959
   231
	// Copy operator. Do we need it?
klao@959
   232
	a=b;
klao@959
   233
klao@959
   234
	ignore_unused_variable_warning(a2);
klao@959
   235
      }
klao@959
   236
      const GraphMap &c;
klao@959
   237
      const Graph &g;
klao@959
   238
      const typename GraphMap::ValueType &t;
klao@959
   239
    };
klao@959
   240
    
klao@959
   241
klao@959
   242
    // @}
klao@959
   243
klao@959
   244
  } //namespace concept
klao@959
   245
} //namespace lemon
klao@959
   246
#endif // LEMON_CONCEPT_MAPS_H