[209] | 1 | /* -*- mode: C++; indent-tabs-mode: nil; -*- |
---|
[25] | 2 | * |
---|
[209] | 3 | * This file is a part of LEMON, a generic C++ optimization library. |
---|
[25] | 4 | * |
---|
[1270] | 5 | * Copyright (C) 2003-2013 |
---|
[25] | 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
| 8 | * |
---|
| 9 | * Permission to use, modify and distribute this software is granted |
---|
| 10 | * provided that this copyright notice appears in all copies. For |
---|
| 11 | * precise terms see the accompanying LICENSE file. |
---|
| 12 | * |
---|
| 13 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 14 | * express or implied, and with no claim as to its suitability for any |
---|
| 15 | * purpose. |
---|
| 16 | * |
---|
| 17 | */ |
---|
| 18 | |
---|
[576] | 19 | #ifndef LEMON_CONCEPTS_MAPS_H |
---|
| 20 | #define LEMON_CONCEPTS_MAPS_H |
---|
[25] | 21 | |
---|
[220] | 22 | #include <lemon/core.h> |
---|
[25] | 23 | #include <lemon/concept_check.h> |
---|
| 24 | |
---|
[314] | 25 | ///\ingroup map_concepts |
---|
[25] | 26 | ///\file |
---|
[114] | 27 | ///\brief The concept of maps. |
---|
[25] | 28 | |
---|
| 29 | namespace lemon { |
---|
| 30 | |
---|
| 31 | namespace concepts { |
---|
[79] | 32 | |
---|
[314] | 33 | /// \addtogroup map_concepts |
---|
[25] | 34 | /// @{ |
---|
| 35 | |
---|
| 36 | /// Readable map concept |
---|
[28] | 37 | |
---|
| 38 | /// Readable map concept. |
---|
| 39 | /// |
---|
[25] | 40 | template<typename K, typename T> |
---|
| 41 | class ReadMap |
---|
| 42 | { |
---|
| 43 | public: |
---|
[35] | 44 | /// The key type of the map. |
---|
[79] | 45 | typedef K Key; |
---|
[210] | 46 | /// \brief The value type of the map. |
---|
| 47 | /// (The type of objects associated with the keys). |
---|
[25] | 48 | typedef T Value; |
---|
| 49 | |
---|
[79] | 50 | /// Returns the value associated with the given key. |
---|
[209] | 51 | Value operator[](const Key &) const { |
---|
[1433] | 52 | // return *(static_cast<Value *>(0)); |
---|
| 53 | // return *(static_cast<Value *>(0)+1); |
---|
| 54 | // return *(static_cast<Value *>(sizeof(Value))); |
---|
| 55 | // return *(reinterpret_cast<Value *>(0)); |
---|
| 56 | // return *(reinterpret_cast<Value *>(0)+1); |
---|
| 57 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
[94] | 58 | } |
---|
[25] | 59 | |
---|
| 60 | template<typename _ReadMap> |
---|
| 61 | struct Constraints { |
---|
[209] | 62 | void constraints() { |
---|
| 63 | Value val = m[key]; |
---|
| 64 | val = m[key]; |
---|
| 65 | typename _ReadMap::Value own_val = m[own_key]; |
---|
| 66 | own_val = m[own_key]; |
---|
[25] | 67 | |
---|
[1257] | 68 | ::lemon::ignore_unused_variable_warning(key); |
---|
| 69 | ::lemon::ignore_unused_variable_warning(val); |
---|
| 70 | ::lemon::ignore_unused_variable_warning(own_key); |
---|
| 71 | ::lemon::ignore_unused_variable_warning(own_val); |
---|
[209] | 72 | } |
---|
| 73 | const Key& key; |
---|
| 74 | const typename _ReadMap::Key& own_key; |
---|
| 75 | const _ReadMap& m; |
---|
[1125] | 76 | Constraints() {} |
---|
[25] | 77 | }; |
---|
[79] | 78 | |
---|
[25] | 79 | }; |
---|
| 80 | |
---|
| 81 | |
---|
| 82 | /// Writable map concept |
---|
[79] | 83 | |
---|
[28] | 84 | /// Writable map concept. |
---|
| 85 | /// |
---|
[25] | 86 | template<typename K, typename T> |
---|
| 87 | class WriteMap |
---|
| 88 | { |
---|
| 89 | public: |
---|
[35] | 90 | /// The key type of the map. |
---|
[79] | 91 | typedef K Key; |
---|
[210] | 92 | /// \brief The value type of the map. |
---|
| 93 | /// (The type of objects associated with the keys). |
---|
[25] | 94 | typedef T Value; |
---|
| 95 | |
---|
[79] | 96 | /// Sets the value associated with the given key. |
---|
| 97 | void set(const Key &, const Value &) {} |
---|
[25] | 98 | |
---|
[79] | 99 | /// Default constructor. |
---|
[25] | 100 | WriteMap() {} |
---|
| 101 | |
---|
| 102 | template <typename _WriteMap> |
---|
| 103 | struct Constraints { |
---|
[209] | 104 | void constraints() { |
---|
| 105 | m.set(key, val); |
---|
| 106 | m.set(own_key, own_val); |
---|
[79] | 107 | |
---|
[1257] | 108 | ::lemon::ignore_unused_variable_warning(key); |
---|
| 109 | ::lemon::ignore_unused_variable_warning(val); |
---|
| 110 | ::lemon::ignore_unused_variable_warning(own_key); |
---|
| 111 | ::lemon::ignore_unused_variable_warning(own_val); |
---|
[209] | 112 | } |
---|
| 113 | const Key& key; |
---|
| 114 | const Value& val; |
---|
| 115 | const typename _WriteMap::Key& own_key; |
---|
| 116 | const typename _WriteMap::Value& own_val; |
---|
| 117 | _WriteMap& m; |
---|
[1125] | 118 | Constraints() {} |
---|
[25] | 119 | }; |
---|
| 120 | }; |
---|
| 121 | |
---|
[48] | 122 | /// Read/writable map concept |
---|
[79] | 123 | |
---|
[28] | 124 | /// Read/writable map concept. |
---|
| 125 | /// |
---|
[25] | 126 | template<typename K, typename T> |
---|
| 127 | class ReadWriteMap : public ReadMap<K,T>, |
---|
[209] | 128 | public WriteMap<K,T> |
---|
[25] | 129 | { |
---|
| 130 | public: |
---|
[35] | 131 | /// The key type of the map. |
---|
[79] | 132 | typedef K Key; |
---|
[210] | 133 | /// \brief The value type of the map. |
---|
| 134 | /// (The type of objects associated with the keys). |
---|
[25] | 135 | typedef T Value; |
---|
| 136 | |
---|
[79] | 137 | /// Returns the value associated with the given key. |
---|
[209] | 138 | Value operator[](const Key &) const { |
---|
[1433] | 139 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
[94] | 140 | } |
---|
[79] | 141 | |
---|
| 142 | /// Sets the value associated with the given key. |
---|
| 143 | void set(const Key &, const Value &) {} |
---|
[25] | 144 | |
---|
| 145 | template<typename _ReadWriteMap> |
---|
| 146 | struct Constraints { |
---|
[209] | 147 | void constraints() { |
---|
| 148 | checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
---|
| 149 | checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
---|
| 150 | } |
---|
[25] | 151 | }; |
---|
| 152 | }; |
---|
[79] | 153 | |
---|
| 154 | |
---|
[28] | 155 | /// Dereferable map concept |
---|
[79] | 156 | |
---|
[28] | 157 | /// Dereferable map concept. |
---|
| 158 | /// |
---|
[25] | 159 | template<typename K, typename T, typename R, typename CR> |
---|
| 160 | class ReferenceMap : public ReadWriteMap<K,T> |
---|
| 161 | { |
---|
| 162 | public: |
---|
| 163 | /// Tag for reference maps. |
---|
| 164 | typedef True ReferenceMapTag; |
---|
[35] | 165 | /// The key type of the map. |
---|
[79] | 166 | typedef K Key; |
---|
[210] | 167 | /// \brief The value type of the map. |
---|
| 168 | /// (The type of objects associated with the keys). |
---|
[25] | 169 | typedef T Value; |
---|
[35] | 170 | /// The reference type of the map. |
---|
[25] | 171 | typedef R Reference; |
---|
[35] | 172 | /// The const reference type of the map. |
---|
[25] | 173 | typedef CR ConstReference; |
---|
| 174 | |
---|
| 175 | public: |
---|
| 176 | |
---|
[79] | 177 | /// Returns a reference to the value associated with the given key. |
---|
[209] | 178 | Reference operator[](const Key &) { |
---|
[1433] | 179 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
[94] | 180 | } |
---|
[79] | 181 | |
---|
| 182 | /// Returns a const reference to the value associated with the given key. |
---|
[94] | 183 | ConstReference operator[](const Key &) const { |
---|
[1433] | 184 | return *(reinterpret_cast<Value *>(sizeof(Value))); |
---|
[94] | 185 | } |
---|
[79] | 186 | |
---|
| 187 | /// Sets the value associated with the given key. |
---|
[25] | 188 | void set(const Key &k,const Value &t) { operator[](k)=t; } |
---|
| 189 | |
---|
| 190 | template<typename _ReferenceMap> |
---|
[74] | 191 | struct Constraints { |
---|
[765] | 192 | typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type |
---|
| 193 | constraints() { |
---|
[209] | 194 | checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
---|
| 195 | ref = m[key]; |
---|
| 196 | m[key] = val; |
---|
| 197 | m[key] = ref; |
---|
| 198 | m[key] = cref; |
---|
| 199 | own_ref = m[own_key]; |
---|
| 200 | m[own_key] = own_val; |
---|
| 201 | m[own_key] = own_ref; |
---|
| 202 | m[own_key] = own_cref; |
---|
| 203 | m[key] = m[own_key]; |
---|
| 204 | m[own_key] = m[key]; |
---|
| 205 | } |
---|
| 206 | const Key& key; |
---|
| 207 | Value& val; |
---|
| 208 | Reference ref; |
---|
| 209 | ConstReference cref; |
---|
| 210 | const typename _ReferenceMap::Key& own_key; |
---|
| 211 | typename _ReferenceMap::Value& own_val; |
---|
| 212 | typename _ReferenceMap::Reference own_ref; |
---|
| 213 | typename _ReferenceMap::ConstReference own_cref; |
---|
| 214 | _ReferenceMap& m; |
---|
[1125] | 215 | Constraints() {} |
---|
[25] | 216 | }; |
---|
| 217 | }; |
---|
| 218 | |
---|
| 219 | // @} |
---|
| 220 | |
---|
| 221 | } //namespace concepts |
---|
[28] | 222 | |
---|
[25] | 223 | } //namespace lemon |
---|
[28] | 224 | |
---|
[576] | 225 | #endif |
---|