[959] | 1 | /* -*- C++ -*- |
---|
| 2 | * |
---|
[1956] | 3 | * This file is a part of LEMON, a generic C++ optimization library |
---|
| 4 | * |
---|
| 5 | * Copyright (C) 2003-2006 |
---|
| 6 | * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
[1359] | 7 | * (Egervary Research Group on Combinatorial Optimization, EGRES). |
---|
[959] | 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 | |
---|
[1993] | 22 | #include <lemon/bits/utility.h> |
---|
[959] | 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 concept { |
---|
| 32 | |
---|
| 33 | /// \addtogroup concept |
---|
| 34 | /// @{ |
---|
| 35 | |
---|
| 36 | /// Readable map concept |
---|
| 37 | template<typename K, typename T> |
---|
| 38 | class ReadMap |
---|
| 39 | { |
---|
| 40 | public: |
---|
| 41 | /// Map's key type. |
---|
[987] | 42 | typedef K Key; |
---|
[959] | 43 | /// Map's value type. (The type of objects associated with the keys). |
---|
[987] | 44 | typedef T Value; |
---|
[959] | 45 | |
---|
[989] | 46 | // \bug Value don't need to be default constructible. |
---|
[959] | 47 | /// Returns the value associated with a key. |
---|
[989] | 48 | Value operator[](const Key &) const {return Value();} |
---|
[959] | 49 | |
---|
[989] | 50 | template<typename _ReadMap> |
---|
| 51 | struct Constraints { |
---|
| 52 | |
---|
| 53 | void constraints() { |
---|
| 54 | Value val = m[key]; |
---|
| 55 | val = m[key]; |
---|
| 56 | typename _ReadMap::Value own_val = m[own_key]; |
---|
| 57 | own_val = m[own_key]; |
---|
| 58 | |
---|
| 59 | ignore_unused_variable_warning(val); |
---|
| 60 | ignore_unused_variable_warning(own_val); |
---|
| 61 | ignore_unused_variable_warning(key); |
---|
| 62 | } |
---|
| 63 | Key& key; |
---|
| 64 | typename _ReadMap::Key& own_key; |
---|
| 65 | _ReadMap& m; |
---|
| 66 | }; |
---|
| 67 | |
---|
[959] | 68 | }; |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | /// Writable map concept |
---|
| 72 | template<typename K, typename T> |
---|
| 73 | class WriteMap |
---|
| 74 | { |
---|
| 75 | public: |
---|
| 76 | /// Map's key type. |
---|
[987] | 77 | typedef K Key; |
---|
[959] | 78 | /// Map's value type. (The type of objects associated with the keys). |
---|
[987] | 79 | typedef T Value; |
---|
[959] | 80 | |
---|
| 81 | /// Sets the value associated with a key. |
---|
[1367] | 82 | void set(const Key &,const Value &) {} |
---|
[959] | 83 | |
---|
| 84 | ///Default constructor |
---|
| 85 | WriteMap() {} |
---|
[989] | 86 | |
---|
| 87 | template <typename _WriteMap> |
---|
| 88 | struct Constraints { |
---|
| 89 | void constraints() { |
---|
| 90 | // No constraints for constructor. |
---|
| 91 | m.set(key, val); |
---|
| 92 | m.set(own_key, own_val); |
---|
[1042] | 93 | ignore_unused_variable_warning(key); |
---|
| 94 | ignore_unused_variable_warning(val); |
---|
| 95 | ignore_unused_variable_warning(own_key); |
---|
| 96 | ignore_unused_variable_warning(own_val); |
---|
[989] | 97 | } |
---|
| 98 | |
---|
| 99 | Value& val; |
---|
| 100 | typename _WriteMap::Value own_val; |
---|
| 101 | Key& key; |
---|
| 102 | typename _WriteMap::Key& own_key; |
---|
[1719] | 103 | _WriteMap& m; |
---|
[989] | 104 | |
---|
| 105 | }; |
---|
[959] | 106 | }; |
---|
| 107 | |
---|
| 108 | ///Read/Writable map concept |
---|
| 109 | template<typename K, typename T> |
---|
| 110 | class ReadWriteMap : public ReadMap<K,T>, |
---|
| 111 | public WriteMap<K,T> |
---|
| 112 | { |
---|
| 113 | public: |
---|
| 114 | /// Map's key type. |
---|
[987] | 115 | typedef K Key; |
---|
[959] | 116 | /// Map's value type. (The type of objects associated with the keys). |
---|
[987] | 117 | typedef T Value; |
---|
[959] | 118 | |
---|
| 119 | /// Returns the value associated with a key. |
---|
[1367] | 120 | Value operator[](const Key &) const {return Value();} |
---|
[959] | 121 | /// Sets the value associated with a key. |
---|
[1367] | 122 | void set(const Key & ,const Value &) {} |
---|
[959] | 123 | |
---|
[989] | 124 | template<typename _ReadWriteMap> |
---|
| 125 | struct Constraints { |
---|
| 126 | void constraints() { |
---|
| 127 | checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
---|
[1719] | 128 | checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
---|
[989] | 129 | } |
---|
| 130 | }; |
---|
[959] | 131 | }; |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | ///Dereferable map concept |
---|
[989] | 135 | template<typename K, typename T, typename R, typename CR> |
---|
[959] | 136 | class ReferenceMap : public ReadWriteMap<K,T> |
---|
| 137 | { |
---|
| 138 | public: |
---|
[1719] | 139 | /// Tag for reference maps. |
---|
| 140 | typedef True ReferenceMapTag; |
---|
[959] | 141 | /// Map's key type. |
---|
[987] | 142 | typedef K Key; |
---|
[959] | 143 | /// Map's value type. (The type of objects associated with the keys). |
---|
[987] | 144 | typedef T Value; |
---|
[989] | 145 | /// Map's reference type. |
---|
| 146 | typedef R Reference; |
---|
| 147 | /// Map's const reference type. |
---|
| 148 | typedef CR ConstReference; |
---|
[959] | 149 | |
---|
| 150 | protected: |
---|
[987] | 151 | Value tmp; |
---|
[959] | 152 | public: |
---|
| 153 | |
---|
| 154 | ///Returns a reference to the value associated to a key. |
---|
[1375] | 155 | Reference operator[](const Key &) { return tmp; } |
---|
[959] | 156 | ///Returns a const reference to the value associated to a key. |
---|
[1375] | 157 | ConstReference operator[](const Key &) const |
---|
[959] | 158 | { return tmp; } |
---|
| 159 | /// Sets the value associated with a key. |
---|
[987] | 160 | void set(const Key &k,const Value &t) { operator[](k)=t; } |
---|
[959] | 161 | |
---|
[989] | 162 | // \todo rethink this concept |
---|
| 163 | template<typename _ReferenceMap> |
---|
| 164 | struct ReferenceMapConcept { |
---|
| 165 | |
---|
| 166 | void constraints() { |
---|
| 167 | checkConcept<ReadWriteMap, _ReferenceMap >(); |
---|
| 168 | m[key] = val; |
---|
| 169 | val = m[key]; |
---|
| 170 | m[key] = ref; |
---|
| 171 | ref = m[key]; |
---|
| 172 | m[own_key] = own_val; |
---|
| 173 | own_val = m[own_key]; |
---|
| 174 | m[own_key] = own_ref; |
---|
| 175 | own_ref = m[own_key]; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | typename _ReferenceMap::Key& own_key; |
---|
| 179 | typename _ReferenceMap::Value& own_val; |
---|
| 180 | typename _ReferenceMap::Reference& own_ref; |
---|
| 181 | Key& key; |
---|
| 182 | Value& val; |
---|
| 183 | Reference& ref; |
---|
[1719] | 184 | _ReferenceMap& m; |
---|
[989] | 185 | }; |
---|
[959] | 186 | }; |
---|
| 187 | |
---|
| 188 | // @} |
---|
| 189 | |
---|
| 190 | } //namespace concept |
---|
| 191 | } //namespace lemon |
---|
| 192 | #endif // LEMON_CONCEPT_MAPS_H |
---|