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