[25] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
| 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 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 | |
---|
| 22 | #include <lemon/bits/utility.h> |
---|
| 23 | #include <lemon/concept_check.h> |
---|
| 24 | |
---|
| 25 | ///\ingroup concept |
---|
| 26 | ///\file |
---|
| 27 | ///\brief Map concepts checking classes for testing and documenting. |
---|
| 28 | |
---|
| 29 | namespace lemon { |
---|
| 30 | |
---|
| 31 | namespace concepts { |
---|
| 32 | |
---|
| 33 | /// \addtogroup concept |
---|
| 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. |
---|
[25] | 45 | typedef K Key; |
---|
[35] | 46 | /// The value type of the map. (The type of objects associated with the keys). |
---|
[25] | 47 | typedef T Value; |
---|
| 48 | |
---|
| 49 | /// Returns the value associated with a key. |
---|
| 50 | |
---|
[48] | 51 | /// Returns the value associated with a key. |
---|
[28] | 52 | /// \bug Value shouldn't need to be default constructible. |
---|
[25] | 53 | /// |
---|
| 54 | Value operator[](const Key &) const {return Value();} |
---|
| 55 | |
---|
| 56 | template<typename _ReadMap> |
---|
| 57 | struct Constraints { |
---|
| 58 | |
---|
| 59 | void constraints() { |
---|
| 60 | Value val = m[key]; |
---|
| 61 | val = m[key]; |
---|
| 62 | typename _ReadMap::Value own_val = m[own_key]; |
---|
| 63 | own_val = m[own_key]; |
---|
| 64 | |
---|
| 65 | ignore_unused_variable_warning(val); |
---|
| 66 | ignore_unused_variable_warning(own_val); |
---|
| 67 | ignore_unused_variable_warning(key); |
---|
| 68 | } |
---|
| 69 | Key& key; |
---|
| 70 | typename _ReadMap::Key& own_key; |
---|
| 71 | _ReadMap& m; |
---|
| 72 | }; |
---|
| 73 | |
---|
| 74 | }; |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | /// Writable map concept |
---|
[28] | 78 | |
---|
| 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. |
---|
[25] | 86 | typedef K Key; |
---|
[35] | 87 | /// The value type of the map. (The type of objects associated with the keys). |
---|
[25] | 88 | typedef T Value; |
---|
| 89 | |
---|
| 90 | /// Sets the value associated with a key. |
---|
| 91 | void set(const Key &,const Value &) {} |
---|
| 92 | |
---|
| 93 | ///Default constructor |
---|
| 94 | WriteMap() {} |
---|
| 95 | |
---|
| 96 | template <typename _WriteMap> |
---|
| 97 | struct Constraints { |
---|
| 98 | void constraints() { |
---|
| 99 | // No constraints for constructor. |
---|
| 100 | m.set(key, val); |
---|
| 101 | m.set(own_key, own_val); |
---|
| 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 | |
---|
| 108 | Value& val; |
---|
| 109 | typename _WriteMap::Value own_val; |
---|
| 110 | Key& key; |
---|
| 111 | typename _WriteMap::Key& own_key; |
---|
| 112 | _WriteMap& m; |
---|
| 113 | |
---|
| 114 | }; |
---|
| 115 | }; |
---|
| 116 | |
---|
[48] | 117 | /// Read/writable map concept |
---|
[28] | 118 | |
---|
| 119 | /// Read/writable map concept. |
---|
| 120 | /// |
---|
[25] | 121 | template<typename K, typename T> |
---|
| 122 | class ReadWriteMap : public ReadMap<K,T>, |
---|
[35] | 123 | public WriteMap<K,T> |
---|
[25] | 124 | { |
---|
| 125 | public: |
---|
[35] | 126 | /// The key type of the map. |
---|
[25] | 127 | typedef K Key; |
---|
[35] | 128 | /// The value type of the map. (The type of objects associated with the keys). |
---|
[25] | 129 | typedef T Value; |
---|
| 130 | |
---|
| 131 | /// Returns the value associated with a key. |
---|
| 132 | Value operator[](const Key &) const {return Value();} |
---|
| 133 | /// Sets the value associated with a key. |
---|
| 134 | void set(const Key & ,const Value &) {} |
---|
| 135 | |
---|
| 136 | template<typename _ReadWriteMap> |
---|
| 137 | struct Constraints { |
---|
| 138 | void constraints() { |
---|
| 139 | checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
---|
| 140 | checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
---|
| 141 | } |
---|
| 142 | }; |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | |
---|
[28] | 146 | /// Dereferable map concept |
---|
| 147 | |
---|
| 148 | /// Dereferable map concept. |
---|
| 149 | /// |
---|
[48] | 150 | /// \todo Rethink this concept. |
---|
[25] | 151 | template<typename K, typename T, typename R, typename CR> |
---|
| 152 | class ReferenceMap : public ReadWriteMap<K,T> |
---|
| 153 | { |
---|
| 154 | public: |
---|
| 155 | /// Tag for reference maps. |
---|
| 156 | typedef True ReferenceMapTag; |
---|
[35] | 157 | /// The key type of the map. |
---|
[25] | 158 | typedef K Key; |
---|
[35] | 159 | /// The value type of the map. (The type of objects associated with the keys). |
---|
[25] | 160 | typedef T Value; |
---|
[35] | 161 | /// The reference type of the map. |
---|
[25] | 162 | typedef R Reference; |
---|
[35] | 163 | /// The const reference type of the map. |
---|
[25] | 164 | typedef CR ConstReference; |
---|
| 165 | |
---|
| 166 | protected: |
---|
| 167 | Value tmp; |
---|
| 168 | public: |
---|
| 169 | |
---|
[48] | 170 | ///Returns a reference to the value associated with a key. |
---|
[25] | 171 | Reference operator[](const Key &) { return tmp; } |
---|
[48] | 172 | ///Returns a const reference to the value associated with a key. |
---|
[28] | 173 | ConstReference operator[](const Key &) const { return tmp; } |
---|
[25] | 174 | /// Sets the value associated with a key. |
---|
| 175 | void set(const Key &k,const Value &t) { operator[](k)=t; } |
---|
| 176 | |
---|
| 177 | template<typename _ReferenceMap> |
---|
| 178 | struct ReferenceMapConcept { |
---|
| 179 | |
---|
| 180 | void constraints() { |
---|
| 181 | checkConcept<ReadWriteMap, _ReferenceMap >(); |
---|
| 182 | m[key] = val; |
---|
| 183 | val = m[key]; |
---|
| 184 | m[key] = ref; |
---|
| 185 | ref = m[key]; |
---|
| 186 | m[own_key] = own_val; |
---|
| 187 | own_val = m[own_key]; |
---|
| 188 | m[own_key] = own_ref; |
---|
| 189 | own_ref = m[own_key]; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | typename _ReferenceMap::Key& own_key; |
---|
| 193 | typename _ReferenceMap::Value& own_val; |
---|
| 194 | typename _ReferenceMap::Reference& own_ref; |
---|
| 195 | Key& key; |
---|
| 196 | Value& val; |
---|
| 197 | Reference& ref; |
---|
| 198 | _ReferenceMap& m; |
---|
| 199 | }; |
---|
| 200 | }; |
---|
| 201 | |
---|
| 202 | // @} |
---|
| 203 | |
---|
| 204 | } //namespace concepts |
---|
[28] | 205 | |
---|
[25] | 206 | } //namespace lemon |
---|
[28] | 207 | |
---|
[25] | 208 | #endif // LEMON_CONCEPT_MAPS_H |
---|