lemon/concepts/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Wed, 17 Oct 2018 22:56:43 +0200
changeset 1196 959d78f3fe0e
parent 1083 3e711ee55d31
child 1211 a278d16bd2d0
permissions -rw-r--r--
Merge #597
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@1092
     5
 * Copyright (C) 2003-2013
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
deba@529
    19
#ifndef LEMON_CONCEPTS_MAPS_H
deba@529
    20
#define LEMON_CONCEPTS_MAPS_H
alpar@25
    21
deba@220
    22
#include <lemon/core.h>
alpar@25
    23
#include <lemon/concept_check.h>
alpar@25
    24
kpeter@314
    25
///\ingroup map_concepts
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
kpeter@314
    33
    /// \addtogroup map_concepts
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;
alpar@210
    46
      /// \brief The value type of the map.
alpar@210
    47
      /// (The type of objects associated with the keys).
alpar@25
    48
      typedef T Value;
alpar@25
    49
kpeter@79
    50
      /// Returns the value associated with the given key.
alpar@209
    51
      Value operator[](const Key &) const {
alpar@997
    52
        return *(static_cast<Value *>(0)+1);
kpeter@94
    53
      }
alpar@25
    54
alpar@25
    55
      template<typename _ReadMap>
alpar@25
    56
      struct Constraints {
alpar@209
    57
        void constraints() {
alpar@209
    58
          Value val = m[key];
alpar@209
    59
          val = m[key];
alpar@209
    60
          typename _ReadMap::Value own_val = m[own_key];
alpar@209
    61
          own_val = m[own_key];
alpar@25
    62
alpar@1083
    63
          ::lemon::ignore_unused_variable_warning(key);
alpar@1083
    64
          ::lemon::ignore_unused_variable_warning(val);
alpar@1083
    65
          ::lemon::ignore_unused_variable_warning(own_key);
alpar@1083
    66
          ::lemon::ignore_unused_variable_warning(own_val);
alpar@209
    67
        }
alpar@209
    68
        const Key& key;
alpar@209
    69
        const typename _ReadMap::Key& own_key;
alpar@209
    70
        const _ReadMap& m;
alpar@975
    71
        Constraints() {}
alpar@25
    72
      };
kpeter@79
    73
alpar@25
    74
    };
alpar@25
    75
alpar@25
    76
alpar@25
    77
    /// Writable map concept
kpeter@79
    78
kpeter@28
    79
    /// Writable map concept.
kpeter@28
    80
    ///
alpar@25
    81
    template<typename K, typename T>
alpar@25
    82
    class WriteMap
alpar@25
    83
    {
alpar@25
    84
    public:
kpeter@35
    85
      /// The key type of the map.
kpeter@79
    86
      typedef K Key;
alpar@210
    87
      /// \brief The value type of the map.
alpar@210
    88
      /// (The type of objects associated with the keys).
alpar@25
    89
      typedef T Value;
alpar@25
    90
kpeter@79
    91
      /// Sets the value associated with the given key.
kpeter@79
    92
      void set(const Key &, const Value &) {}
alpar@25
    93
kpeter@79
    94
      /// Default constructor.
alpar@25
    95
      WriteMap() {}
alpar@25
    96
alpar@25
    97
      template <typename _WriteMap>
alpar@25
    98
      struct Constraints {
alpar@209
    99
        void constraints() {
alpar@209
   100
          m.set(key, val);
alpar@209
   101
          m.set(own_key, own_val);
kpeter@79
   102
alpar@1083
   103
          ::lemon::ignore_unused_variable_warning(key);
alpar@1083
   104
          ::lemon::ignore_unused_variable_warning(val);
alpar@1083
   105
          ::lemon::ignore_unused_variable_warning(own_key);
alpar@1083
   106
          ::lemon::ignore_unused_variable_warning(own_val);
alpar@209
   107
        }
alpar@209
   108
        const Key& key;
alpar@209
   109
        const Value& val;
alpar@209
   110
        const typename _WriteMap::Key& own_key;
alpar@209
   111
        const typename _WriteMap::Value& own_val;
alpar@209
   112
        _WriteMap& m;
alpar@975
   113
        Constraints() {}
alpar@25
   114
      };
alpar@25
   115
    };
alpar@25
   116
kpeter@48
   117
    /// Read/writable map concept
kpeter@79
   118
kpeter@28
   119
    /// Read/writable map concept.
kpeter@28
   120
    ///
alpar@25
   121
    template<typename K, typename T>
alpar@25
   122
    class ReadWriteMap : public ReadMap<K,T>,
alpar@209
   123
                         public WriteMap<K,T>
alpar@25
   124
    {
alpar@25
   125
    public:
kpeter@35
   126
      /// The key type of the map.
kpeter@79
   127
      typedef K Key;
alpar@210
   128
      /// \brief The value type of the map.
alpar@210
   129
      /// (The type of objects associated with the keys).
alpar@25
   130
      typedef T Value;
alpar@25
   131
kpeter@79
   132
      /// Returns the value associated with the given key.
alpar@209
   133
      Value operator[](const Key &) const {
alpar@975
   134
        Value *r = 0;
alpar@975
   135
        return *r;
kpeter@94
   136
      }
kpeter@79
   137
kpeter@79
   138
      /// Sets the value associated with the given key.
kpeter@79
   139
      void set(const Key &, const Value &) {}
alpar@25
   140
alpar@25
   141
      template<typename _ReadWriteMap>
alpar@25
   142
      struct Constraints {
alpar@209
   143
        void constraints() {
alpar@209
   144
          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
alpar@209
   145
          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
alpar@209
   146
        }
alpar@25
   147
      };
alpar@25
   148
    };
kpeter@79
   149
kpeter@79
   150
kpeter@28
   151
    /// Dereferable map concept
kpeter@79
   152
kpeter@28
   153
    /// Dereferable map concept.
kpeter@28
   154
    ///
alpar@25
   155
    template<typename K, typename T, typename R, typename CR>
alpar@25
   156
    class ReferenceMap : public ReadWriteMap<K,T>
alpar@25
   157
    {
alpar@25
   158
    public:
alpar@25
   159
      /// Tag for reference maps.
alpar@25
   160
      typedef True ReferenceMapTag;
kpeter@35
   161
      /// The key type of the map.
kpeter@79
   162
      typedef K Key;
alpar@210
   163
      /// \brief The value type of the map.
alpar@210
   164
      /// (The type of objects associated with the keys).
alpar@25
   165
      typedef T Value;
kpeter@35
   166
      /// The reference type of the map.
alpar@25
   167
      typedef R Reference;
kpeter@35
   168
      /// The const reference type of the map.
alpar@25
   169
      typedef CR ConstReference;
alpar@25
   170
alpar@25
   171
    public:
alpar@25
   172
kpeter@79
   173
      /// Returns a reference to the value associated with the given key.
alpar@209
   174
      Reference operator[](const Key &) {
alpar@975
   175
        Value *r = 0;
alpar@975
   176
        return *r;
kpeter@94
   177
      }
kpeter@79
   178
kpeter@79
   179
      /// Returns a const reference to the value associated with the given key.
kpeter@94
   180
      ConstReference operator[](const Key &) const {
alpar@975
   181
        Value *r = 0;
alpar@975
   182
        return *r;
kpeter@94
   183
      }
kpeter@79
   184
kpeter@79
   185
      /// Sets the value associated with the given key.
alpar@25
   186
      void set(const Key &k,const Value &t) { operator[](k)=t; }
alpar@25
   187
alpar@25
   188
      template<typename _ReferenceMap>
kpeter@74
   189
      struct Constraints {
kpeter@718
   190
        typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
kpeter@718
   191
        constraints() {
alpar@209
   192
          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
alpar@209
   193
          ref = m[key];
alpar@209
   194
          m[key] = val;
alpar@209
   195
          m[key] = ref;
alpar@209
   196
          m[key] = cref;
alpar@209
   197
          own_ref = m[own_key];
alpar@209
   198
          m[own_key] = own_val;
alpar@209
   199
          m[own_key] = own_ref;
alpar@209
   200
          m[own_key] = own_cref;
alpar@209
   201
          m[key] = m[own_key];
alpar@209
   202
          m[own_key] = m[key];
alpar@209
   203
        }
alpar@209
   204
        const Key& key;
alpar@209
   205
        Value& val;
alpar@209
   206
        Reference ref;
alpar@209
   207
        ConstReference cref;
alpar@209
   208
        const typename _ReferenceMap::Key& own_key;
alpar@209
   209
        typename _ReferenceMap::Value& own_val;
alpar@209
   210
        typename _ReferenceMap::Reference own_ref;
alpar@209
   211
        typename _ReferenceMap::ConstReference own_cref;
alpar@209
   212
        _ReferenceMap& m;
alpar@975
   213
        Constraints() {}
alpar@25
   214
      };
alpar@25
   215
    };
alpar@25
   216
alpar@25
   217
    // @}
alpar@25
   218
alpar@25
   219
  } //namespace concepts
kpeter@28
   220
alpar@25
   221
} //namespace lemon
kpeter@28
   222
deba@529
   223
#endif