1 /* -*- C++ -*- 2 * 3 * This file is a part of LEMON, a generic C++ optimization library 4 * 5 * Copyright (C) 2003-2008 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 37 38 /// Readable map concept. 39 /// 40 template<typename K, typename T> 41 class ReadMap 42 { 43 public: 44 /// The key type of the map. 45 typedef K Key; 46 /// The value type of the map. (The type of objects associated with the keys). 47 typedef T Value; 48 49 /// Returns the value associated with a key. 50 51 /// Returns the value associated with a key. 52 /// \bug Value shouldn't need to be default constructible. 53 /// 54 Value operator[](const Key &) const {return Value();} 55 56 template<typename _ReadMap> 57 struct Constraints { 58 constraintsConstraints59 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 78 79 /// Writable map concept. 80 /// 81 template<typename K, typename T> 82 class WriteMap 83 { 84 public: 85 /// The key type of the map. 86 typedef K Key; 87 /// The value type of the map. (The type of objects associated with the keys). 88 typedef T Value; 89 90 /// Sets the value associated with a key. set(const Key &,const Value &)91 void set(const Key &,const Value &) {} 92 93 ///Default constructor WriteMap()94 WriteMap() {} 95 96 template <typename _WriteMap> 97 struct Constraints { constraintsConstraints98 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 117 /// Read/writable map concept 118 119 /// Read/writable map concept. 120 /// 121 template<typename K, typename T> 122 class ReadWriteMap : public ReadMap<K,T>, 123 public WriteMap<K,T> 124 { 125 public: 126 /// The key type of the map. 127 typedef K Key; 128 /// The value type of the map. (The type of objects associated with the keys). 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. set(const Key &,const Value &)134 void set(const Key & ,const Value &) {} 135 136 template<typename _ReadWriteMap> 137 struct Constraints { constraintsConstraints138 void constraints() { 139 checkConcept<ReadMap<K, T>, _ReadWriteMap >(); 140 checkConcept<WriteMap<K, T>, _ReadWriteMap >(); 141 } 142 }; 143 }; 144 145 146 ///Dereferable map concept 147 148 /// Dereferable map concept. 149 /// 150 /// \todo Rethink this concept. 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; 157 /// The key type of the map. 158 typedef K Key; 159 /// The value type of the map. (The type of objects associated with the keys). 160 typedef T Value; 161 /// The reference type of the map. 162 typedef R Reference; 163 /// The const reference type of the map. 164 typedef CR ConstReference; 165 166 protected: 167 Value tmp; 168 public: 169 170 ///Returns a reference to the value associated with a key. 171 Reference operator[](const Key &) { return tmp; } 172 ///Returns a const reference to the value associated with a key. 173 ConstReference operator[](const Key &) const { return tmp; } 174 /// Sets the value associated with a key. set(const Key & k,const Value & t)175 void set(const Key &k,const Value &t) { operator[](k)=t; } 176 177 template<typename _ReferenceMap> 178 struct Constraints { 179 constraintsConstraints180 void constraints() { 181 checkConcept<ReadWriteMap<K, T>, _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 205 206 } //namespace lemon 207 208 #endif // LEMON_CONCEPT_MAPS_H 209