lemon/concepts/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Sun, 13 Jul 2008 19:51:02 +0100
changeset 209 765619b7cbb2
parent 114 c837d1e449dc
child 210 81cfc04531e8
permissions -rw-r--r--
Apply unify-sources.sh to the source tree
alpar@209
     1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@25
     2
 *
alpar@209
     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
kpeter@114
    27
///\brief The concept of maps.
alpar@25
    28
alpar@25
    29
namespace lemon {
alpar@25
    30
alpar@25
    31
  namespace concepts {
kpeter@79
    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.
kpeter@79
    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
kpeter@79
    49
      /// Returns the value associated with the given key.
alpar@209
    50
      Value operator[](const Key &) const {
kpeter@94
    51
        return *static_cast<Value *>(0);
kpeter@94
    52
      }
alpar@25
    53
alpar@25
    54
      template<typename _ReadMap>
alpar@25
    55
      struct Constraints {
alpar@209
    56
        void constraints() {
alpar@209
    57
          Value val = m[key];
alpar@209
    58
          val = m[key];
alpar@209
    59
          typename _ReadMap::Value own_val = m[own_key];
alpar@209
    60
          own_val = m[own_key];
alpar@25
    61
alpar@209
    62
          ignore_unused_variable_warning(key);
alpar@209
    63
          ignore_unused_variable_warning(val);
alpar@209
    64
          ignore_unused_variable_warning(own_key);
alpar@209
    65
          ignore_unused_variable_warning(own_val);
alpar@209
    66
        }
alpar@209
    67
        const Key& key;
alpar@209
    68
        const typename _ReadMap::Key& own_key;
alpar@209
    69
        const _ReadMap& m;
alpar@25
    70
      };
kpeter@79
    71
alpar@25
    72
    };
alpar@25
    73
alpar@25
    74
alpar@25
    75
    /// Writable map concept
kpeter@79
    76
kpeter@28
    77
    /// Writable map concept.
kpeter@28
    78
    ///
alpar@25
    79
    template<typename K, typename T>
alpar@25
    80
    class WriteMap
alpar@25
    81
    {
alpar@25
    82
    public:
kpeter@35
    83
      /// The key type of the map.
kpeter@79
    84
      typedef K Key;
kpeter@35
    85
      /// The value type of the map. (The type of objects associated with the keys).
alpar@25
    86
      typedef T Value;
alpar@25
    87
kpeter@79
    88
      /// Sets the value associated with the given key.
kpeter@79
    89
      void set(const Key &, const Value &) {}
alpar@25
    90
kpeter@79
    91
      /// Default constructor.
alpar@25
    92
      WriteMap() {}
alpar@25
    93
alpar@25
    94
      template <typename _WriteMap>
alpar@25
    95
      struct Constraints {
alpar@209
    96
        void constraints() {
alpar@209
    97
          m.set(key, val);
alpar@209
    98
          m.set(own_key, own_val);
kpeter@79
    99
alpar@209
   100
          ignore_unused_variable_warning(key);
alpar@209
   101
          ignore_unused_variable_warning(val);
alpar@209
   102
          ignore_unused_variable_warning(own_key);
alpar@209
   103
          ignore_unused_variable_warning(own_val);
alpar@209
   104
        }
alpar@209
   105
        const Key& key;
alpar@209
   106
        const Value& val;
alpar@209
   107
        const typename _WriteMap::Key& own_key;
alpar@209
   108
        const typename _WriteMap::Value& own_val;
alpar@209
   109
        _WriteMap& m;
alpar@25
   110
      };
alpar@25
   111
    };
alpar@25
   112
kpeter@48
   113
    /// Read/writable map concept
kpeter@79
   114
kpeter@28
   115
    /// Read/writable map concept.
kpeter@28
   116
    ///
alpar@25
   117
    template<typename K, typename T>
alpar@25
   118
    class ReadWriteMap : public ReadMap<K,T>,
alpar@209
   119
                         public WriteMap<K,T>
alpar@25
   120
    {
alpar@25
   121
    public:
kpeter@35
   122
      /// The key type of the map.
kpeter@79
   123
      typedef K Key;
kpeter@35
   124
      /// The value type of the map. (The type of objects associated with the keys).
alpar@25
   125
      typedef T Value;
alpar@25
   126
kpeter@79
   127
      /// Returns the value associated with the given key.
alpar@209
   128
      Value operator[](const Key &) const {
kpeter@94
   129
        return *static_cast<Value *>(0);
kpeter@94
   130
      }
kpeter@79
   131
kpeter@79
   132
      /// Sets the value associated with the given key.
kpeter@79
   133
      void set(const Key &, const Value &) {}
alpar@25
   134
alpar@25
   135
      template<typename _ReadWriteMap>
alpar@25
   136
      struct Constraints {
alpar@209
   137
        void constraints() {
alpar@209
   138
          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
alpar@209
   139
          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
alpar@209
   140
        }
alpar@25
   141
      };
alpar@25
   142
    };
kpeter@79
   143
kpeter@79
   144
kpeter@28
   145
    /// Dereferable map concept
kpeter@79
   146
kpeter@28
   147
    /// Dereferable map concept.
kpeter@28
   148
    ///
alpar@25
   149
    template<typename K, typename T, typename R, typename CR>
alpar@25
   150
    class ReferenceMap : public ReadWriteMap<K,T>
alpar@25
   151
    {
alpar@25
   152
    public:
alpar@25
   153
      /// Tag for reference maps.
alpar@25
   154
      typedef True ReferenceMapTag;
kpeter@35
   155
      /// The key type of the map.
kpeter@79
   156
      typedef K Key;
kpeter@35
   157
      /// The value type of the map. (The type of objects associated with the keys).
alpar@25
   158
      typedef T Value;
kpeter@35
   159
      /// The reference type of the map.
alpar@25
   160
      typedef R Reference;
kpeter@35
   161
      /// The const reference type of the map.
alpar@25
   162
      typedef CR ConstReference;
alpar@25
   163
alpar@25
   164
    public:
alpar@25
   165
kpeter@79
   166
      /// Returns a reference to the value associated with the given key.
alpar@209
   167
      Reference operator[](const Key &) {
kpeter@94
   168
        return *static_cast<Value *>(0);
kpeter@94
   169
      }
kpeter@79
   170
kpeter@79
   171
      /// Returns a const reference to the value associated with the given key.
kpeter@94
   172
      ConstReference operator[](const Key &) const {
kpeter@94
   173
        return *static_cast<Value *>(0);
kpeter@94
   174
      }
kpeter@79
   175
kpeter@79
   176
      /// Sets the value associated with the given key.
alpar@25
   177
      void set(const Key &k,const Value &t) { operator[](k)=t; }
alpar@25
   178
alpar@25
   179
      template<typename _ReferenceMap>
kpeter@74
   180
      struct Constraints {
alpar@209
   181
        void constraints() {
alpar@209
   182
          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
alpar@209
   183
          ref = m[key];
alpar@209
   184
          m[key] = val;
alpar@209
   185
          m[key] = ref;
alpar@209
   186
          m[key] = cref;
alpar@209
   187
          own_ref = m[own_key];
alpar@209
   188
          m[own_key] = own_val;
alpar@209
   189
          m[own_key] = own_ref;
alpar@209
   190
          m[own_key] = own_cref;
alpar@209
   191
          m[key] = m[own_key];
alpar@209
   192
          m[own_key] = m[key];
alpar@209
   193
        }
alpar@209
   194
        const Key& key;
alpar@209
   195
        Value& val;
alpar@209
   196
        Reference ref;
alpar@209
   197
        ConstReference cref;
alpar@209
   198
        const typename _ReferenceMap::Key& own_key;
alpar@209
   199
        typename _ReferenceMap::Value& own_val;
alpar@209
   200
        typename _ReferenceMap::Reference own_ref;
alpar@209
   201
        typename _ReferenceMap::ConstReference own_cref;
alpar@209
   202
        _ReferenceMap& m;
alpar@25
   203
      };
alpar@25
   204
    };
alpar@25
   205
alpar@25
   206
    // @}
alpar@25
   207
alpar@25
   208
  } //namespace concepts
kpeter@28
   209
alpar@25
   210
} //namespace lemon
kpeter@28
   211
alpar@25
   212
#endif // LEMON_CONCEPT_MAPS_H