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