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