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