src/lemon/skeletons/maps.h
author alpar
Sat, 30 Oct 2004 18:51:00 +0000
changeset 951 0f1fe84ff36c
parent 921 818510fa3d99
permissions -rw-r--r--
- SmallGraph is also a class instead of being a typedef.
(For the sake of doxygen.)
alpar@906
     1
/* -*- C++ -*-
alpar@921
     2
 * src/lemon/skeletons/maps.h - Part of LEMON, a generic C++ optimization library
alpar@906
     3
 *
alpar@906
     4
 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906
     5
 * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906
     6
 *
alpar@906
     7
 * Permission to use, modify and distribute this software is granted
alpar@906
     8
 * provided that this copyright notice appears in all copies. For
alpar@906
     9
 * precise terms see the accompanying LICENSE file.
alpar@906
    10
 *
alpar@906
    11
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    12
 * express or implied, and with no claim as to its suitability for any
alpar@906
    13
 * purpose.
alpar@906
    14
 *
alpar@906
    15
 */
alpar@906
    16
alpar@921
    17
#ifndef LEMON_MAPSKELETON_H
alpar@921
    18
#define LEMON_MAPSKELETON_H
alpar@186
    19
klao@946
    20
#include <lemon/concept_check.h>
klao@946
    21
alpar@794
    22
///\ingroup skeletons
alpar@242
    23
///\file
alpar@242
    24
///\brief Map concepts checking classes for testing and documenting.
alpar@242
    25
alpar@921
    26
namespace lemon {
klao@282
    27
klao@282
    28
  namespace skeleton {
alpar@186
    29
  
alpar@794
    30
    /// \addtogroup skeletons
alpar@794
    31
    /// @{
alpar@794
    32
klao@282
    33
    /// Readable map concept
klao@282
    34
    template<typename K, typename T>
alpar@732
    35
    class ReadMap
klao@282
    36
    {
klao@282
    37
    public:
klao@282
    38
      /// Map's key type.
klao@282
    39
      typedef K KeyType;    
klao@282
    40
      /// Map's value type. (The type of objects associated with the keys).
klao@282
    41
      typedef T ValueType;
alpar@186
    42
klao@282
    43
      /// Returns the value associated with a key.
klao@282
    44
      ValueType operator[](const KeyType &k) const {return ValueType();}
alpar@186
    45
alpar@732
    46
      ///Default constructor
alpar@732
    47
      ReadMap() {}
klao@282
    48
    };
klao@282
    49
klao@282
    50
klao@282
    51
    /// Writable map concept
klao@282
    52
    template<typename K, typename T>
alpar@732
    53
    class WriteMap
klao@282
    54
    {
klao@282
    55
    public:
klao@282
    56
      /// Map's key type.
klao@282
    57
      typedef K KeyType;    
klao@282
    58
      /// Map's value type. (The type of objects associated with the keys).
klao@282
    59
      typedef T ValueType;
klao@282
    60
klao@282
    61
      /// Sets the value associated with a key.
klao@282
    62
      void set(const KeyType &k,const ValueType &t) {}
klao@282
    63
alpar@732
    64
      ///Default constructor
alpar@732
    65
      WriteMap() {}
klao@282
    66
    };
klao@282
    67
alpar@809
    68
    ///Read/Writable map concept
klao@282
    69
    template<typename K, typename T>
alpar@732
    70
    class ReadWriteMap : public ReadMap<K,T>,
alpar@732
    71
			    public WriteMap<K,T>
klao@282
    72
    {
klao@282
    73
    public:
klao@282
    74
      /// Map's key type.
klao@282
    75
      typedef K KeyType;    
klao@282
    76
      /// Map's value type. (The type of objects associated with the keys).
klao@282
    77
      typedef T ValueType;
klao@282
    78
klao@282
    79
      /// Returns the value associated with a key.
klao@282
    80
      ValueType operator[](const KeyType &k) const {return ValueType();}
klao@282
    81
      /// Sets the value associated with a key.
klao@282
    82
      void set(const KeyType &k,const ValueType &t) {}
klao@282
    83
alpar@732
    84
      ///Default constructor
alpar@732
    85
      ReadWriteMap() {}
klao@282
    86
    };
alpar@186
    87
  
alpar@186
    88
  
klao@282
    89
    ///Dereferable map concept
klao@282
    90
    template<typename K, typename T>
alpar@732
    91
    class ReferenceMap : public ReadWriteMap<K,T>
klao@282
    92
    {
klao@282
    93
    public:
klao@282
    94
      /// Map's key type.
klao@282
    95
      typedef K KeyType;    
klao@282
    96
      /// Map's value type. (The type of objects associated with the keys).
klao@282
    97
      typedef T ValueType;
alpar@732
    98
alpar@732
    99
    protected:
alpar@732
   100
      ValueType tmp;
alpar@732
   101
    public:
klao@282
   102
      typedef ValueType& ReferenceType;
klao@282
   103
      /// Map's const reference type.
klao@282
   104
      typedef const ValueType& ConstReferenceType;
alpar@186
   105
klao@282
   106
      ///Returns a reference to the value associated to a key.
alpar@732
   107
      ReferenceType operator[](const KeyType &i) { return tmp; }
klao@282
   108
      ///Returns a const reference to the value associated to a key.
alpar@732
   109
      ConstReferenceType operator[](const KeyType &i) const
alpar@732
   110
      { return tmp; }
klao@282
   111
      /// Sets the value associated with a key.
klao@282
   112
      void set(const KeyType &k,const ValueType &t) { operator[](k)=t; }
alpar@186
   113
alpar@732
   114
      ///Default constructor
alpar@732
   115
      ReferenceMap() {}
klao@282
   116
    };
alpar@794
   117
klao@946
   118
klao@946
   119
    template<typename Item, typename T, typename Graph>
klao@946
   120
    class GraphMap : public ReadWriteMap<Item, T> {
klao@946
   121
      // I really, really don't like the idea that every graph should have
klao@946
   122
      // reference maps! --klao
klao@946
   123
klao@946
   124
    private:
klao@946
   125
      // We state explicitly that graph maps have no default constructor?
klao@946
   126
      GraphMap();
klao@946
   127
klao@946
   128
    public:
klao@946
   129
      explicit GraphMap(Graph const&) {}
klao@946
   130
      // value for initializing
klao@946
   131
      GraphMap(Graph const&, T) {}
klao@946
   132
klao@946
   133
      // this probably should be required:
klao@946
   134
      GraphMap(GraphMap const&) {}
klao@946
   135
      GraphMap& operator=(GraphMap const&) { return *this; }
klao@946
   136
klao@946
   137
      // but this is a absolute no-op! We should provide a more generic
klao@946
   138
      // graph-map-copy operation.
klao@946
   139
      //
klao@946
   140
      // template<typename TT>
klao@946
   141
      // GraphMap(GraphMap<TT> const&);
klao@946
   142
      //
klao@946
   143
      // template<typename TT>
klao@946
   144
      // GraphMap& operator=(const GraphMap<TT>&);
klao@946
   145
    };
klao@946
   146
klao@946
   147
klao@946
   148
    /****************  Concept-checking classes  ****************/
klao@946
   149
klao@946
   150
    template<typename ReadMap>
klao@946
   151
    struct ReadMapConcept {
klao@946
   152
      typedef typename ReadMap::KeyType KeyType;
klao@946
   153
      typedef typename ReadMap::ValueType ValueType;
klao@946
   154
klao@946
   155
      void constraints() {
klao@946
   156
	// No constraints for constructor.
klao@946
   157
klao@946
   158
	// What are the requirement for the ValueType?
klao@946
   159
	// CopyConstructible? Assignable? None of these?
klao@946
   160
	ValueType v = m[k];
klao@946
   161
	v = m[k];
klao@946
   162
klao@946
   163
	// FIXME:
klao@946
   164
	ignore_unused_variable_warning(v);
klao@946
   165
      }
klao@946
   166
klao@946
   167
      ReadMap m;
klao@946
   168
      KeyType k;
klao@946
   169
    };
klao@946
   170
klao@946
   171
    template<typename WriteMap>
klao@946
   172
    struct WriteMapConcept {
klao@946
   173
      typedef typename WriteMap::KeyType KeyType;
klao@946
   174
      typedef typename WriteMap::ValueType ValueType;
klao@946
   175
klao@946
   176
      void constraints() {
klao@946
   177
	// No constraints for constructor.
klao@946
   178
klao@946
   179
	m.set(k, v);
klao@946
   180
      }
klao@946
   181
klao@946
   182
      WriteMap m;
klao@946
   183
      KeyType k;
klao@946
   184
      ValueType v;
klao@946
   185
    };
klao@946
   186
klao@946
   187
    template<typename ReadWriteMap>
klao@946
   188
    struct ReadWriteMapConcept {
klao@946
   189
      void constraints() {
klao@946
   190
	function_requires< ReadMapConcept<ReadWriteMap> >();
klao@946
   191
	function_requires< WriteMapConcept<ReadWriteMap> >();
klao@946
   192
      }
klao@946
   193
    };
klao@946
   194
klao@946
   195
    template<typename ReferenceMap>
klao@946
   196
    struct ReferenceMapConcept {
klao@946
   197
      typedef typename ReferenceMap::KeyType KeyType;
klao@946
   198
      typedef typename ReferenceMap::ValueType ValueType;
klao@946
   199
      typedef typename ReferenceMap::ReferenceType ReferenceType;
klao@946
   200
klao@946
   201
      // What for is this?
klao@946
   202
      typedef typename ReferenceMap::ConstReferenceType ConstReferenceType;
klao@946
   203
klao@946
   204
      void constraints() {
klao@946
   205
	function_requires< ReadWriteMapConcept<ReferenceMap> >();
klao@946
   206
klao@946
   207
	m[k] = v;
klao@946
   208
	// Or should we require real reference?
klao@946
   209
	// Like this:
klao@946
   210
	// ValueType &vv = m[k];
klao@946
   211
	// ignore_unused_variable_warning(vv);
klao@946
   212
      }
klao@946
   213
klao@946
   214
      ReferenceMap m;
klao@946
   215
      KeyType k;
klao@946
   216
      ValueType v;
klao@946
   217
    };
klao@946
   218
klao@946
   219
    /// \todo GraphMapConceptCheck
klao@946
   220
klao@946
   221
    template<typename GraphMap, typename Graph>
klao@946
   222
    struct GraphMapConcept {
klao@946
   223
      void constraints() {
klao@946
   224
	function_requires< ReadWriteMapConcept<GraphMap> >();
klao@946
   225
	// Construction with a graph parameter
klao@946
   226
	GraphMap a(g);
klao@946
   227
	// Ctor with a graph and a default value parameter
klao@946
   228
	GraphMap a2(g,t);
klao@946
   229
	// Copy ctor. Do we need it?
klao@946
   230
	GraphMap b=c;
klao@946
   231
	// Copy operator. Do we need it?
klao@946
   232
	a=b;
klao@946
   233
klao@946
   234
	ignore_unused_variable_warning(a2);
klao@946
   235
      }
klao@946
   236
      const GraphMap &c;
klao@946
   237
      const Graph &g;
klao@946
   238
      const typename GraphMap::ValueType &t;
klao@946
   239
    };
klao@946
   240
    
klao@946
   241
alpar@794
   242
    // @}
alpar@794
   243
alpar@732
   244
  } //namespace skeleton
alpar@921
   245
} //namespace lemon
alpar@921
   246
#endif // LEMON_MAPSKELETON_H