0
2
0
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_BITS_MAP_EXTENDER_H |
20 | 20 |
#define LEMON_BITS_MAP_EXTENDER_H |
21 | 21 |
|
22 | 22 |
#include <iterator> |
23 | 23 |
|
24 | 24 |
#include <lemon/bits/traits.h> |
25 | 25 |
|
26 | 26 |
#include <lemon/concept_check.h> |
27 | 27 |
#include <lemon/concepts/maps.h> |
28 | 28 |
|
29 | 29 |
//\file |
30 | 30 |
//\brief Extenders for iterable maps. |
31 | 31 |
|
32 | 32 |
namespace lemon { |
33 | 33 |
|
34 | 34 |
// \ingroup graphbits |
35 | 35 |
// |
36 | 36 |
// \brief Extender for maps |
37 | 37 |
template <typename _Map> |
38 | 38 |
class MapExtender : public _Map { |
39 | 39 |
typedef _Map Parent; |
40 | 40 |
typedef typename Parent::GraphType GraphType; |
41 | 41 |
|
42 | 42 |
public: |
43 | 43 |
|
44 | 44 |
typedef MapExtender Map; |
45 | 45 |
typedef typename Parent::Key Item; |
46 | 46 |
|
47 | 47 |
typedef typename Parent::Key Key; |
48 | 48 |
typedef typename Parent::Value Value; |
49 | 49 |
typedef typename Parent::Reference Reference; |
50 | 50 |
typedef typename Parent::ConstReference ConstReference; |
51 | 51 |
|
52 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
|
53 |
|
|
52 | 54 |
class MapIt; |
53 | 55 |
class ConstMapIt; |
54 | 56 |
|
55 | 57 |
friend class MapIt; |
56 | 58 |
friend class ConstMapIt; |
57 | 59 |
|
58 | 60 |
public: |
59 | 61 |
|
60 | 62 |
MapExtender(const GraphType& graph) |
61 | 63 |
: Parent(graph) {} |
62 | 64 |
|
63 | 65 |
MapExtender(const GraphType& graph, const Value& value) |
64 | 66 |
: Parent(graph, value) {} |
65 | 67 |
|
66 | 68 |
private: |
67 | 69 |
MapExtender& operator=(const MapExtender& cmap) { |
68 | 70 |
return operator=<MapExtender>(cmap); |
69 | 71 |
} |
70 | 72 |
|
71 | 73 |
template <typename CMap> |
72 | 74 |
MapExtender& operator=(const CMap& cmap) { |
73 | 75 |
Parent::operator=(cmap); |
74 | 76 |
return *this; |
75 | 77 |
} |
76 | 78 |
|
77 | 79 |
public: |
78 | 80 |
class MapIt : public Item { |
79 | 81 |
typedef Item Parent; |
80 | 82 |
|
81 | 83 |
public: |
82 | 84 |
|
83 | 85 |
typedef typename Map::Value Value; |
84 | 86 |
|
85 | 87 |
MapIt() {} |
86 | 88 |
|
87 | 89 |
MapIt(Invalid i) : Parent(i) { } |
88 | 90 |
|
89 | 91 |
explicit MapIt(Map& _map) : map(_map) { |
90 | 92 |
map.notifier()->first(*this); |
91 | 93 |
} |
92 | 94 |
|
93 | 95 |
MapIt(const Map& _map, const Item& item) |
94 | 96 |
: Parent(item), map(_map) {} |
95 | 97 |
|
96 | 98 |
MapIt& operator++() { |
97 | 99 |
map.notifier()->next(*this); |
98 | 100 |
return *this; |
99 | 101 |
} |
100 | 102 |
|
101 | 103 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
102 | 104 |
return map[*this]; |
103 | 105 |
} |
104 | 106 |
|
105 | 107 |
typename MapTraits<Map>::ReturnValue operator*() { |
106 | 108 |
return map[*this]; |
107 | 109 |
} |
108 | 110 |
|
109 | 111 |
void set(const Value& value) { |
110 | 112 |
map.set(*this, value); |
111 | 113 |
} |
112 | 114 |
|
113 | 115 |
protected: |
114 | 116 |
Map& map; |
115 | 117 |
|
116 | 118 |
}; |
117 | 119 |
|
118 | 120 |
class ConstMapIt : public Item { |
119 | 121 |
typedef Item Parent; |
120 | 122 |
|
121 | 123 |
public: |
122 | 124 |
|
123 | 125 |
typedef typename Map::Value Value; |
124 | 126 |
|
125 | 127 |
ConstMapIt() {} |
126 | 128 |
|
127 | 129 |
ConstMapIt(Invalid i) : Parent(i) { } |
128 | 130 |
|
129 | 131 |
explicit ConstMapIt(Map& _map) : map(_map) { |
130 | 132 |
map.notifier()->first(*this); |
131 | 133 |
} |
132 | 134 |
|
133 | 135 |
ConstMapIt(const Map& _map, const Item& item) |
134 | 136 |
: Parent(item), map(_map) {} |
135 | 137 |
|
136 | 138 |
ConstMapIt& operator++() { |
137 | 139 |
map.notifier()->next(*this); |
138 | 140 |
return *this; |
139 | 141 |
} |
140 | 142 |
|
141 | 143 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
142 | 144 |
return map[*this]; |
143 | 145 |
} |
144 | 146 |
|
145 | 147 |
protected: |
146 | 148 |
const Map& map; |
147 | 149 |
}; |
148 | 150 |
|
149 | 151 |
class ItemIt : public Item { |
150 | 152 |
typedef Item Parent; |
151 | 153 |
|
152 | 154 |
public: |
153 | 155 |
|
154 | 156 |
ItemIt() {} |
155 | 157 |
|
156 | 158 |
ItemIt(Invalid i) : Parent(i) { } |
157 | 159 |
|
158 | 160 |
explicit ItemIt(Map& _map) : map(_map) { |
159 | 161 |
map.notifier()->first(*this); |
160 | 162 |
} |
161 | 163 |
|
162 | 164 |
ItemIt(const Map& _map, const Item& item) |
163 | 165 |
: Parent(item), map(_map) {} |
164 | 166 |
|
165 | 167 |
ItemIt& operator++() { |
166 | 168 |
map.notifier()->next(*this); |
167 | 169 |
return *this; |
168 | 170 |
} |
169 | 171 |
|
170 | 172 |
protected: |
171 | 173 |
const Map& map; |
172 | 174 |
|
173 | 175 |
}; |
174 | 176 |
}; |
175 | 177 |
|
176 | 178 |
// \ingroup graphbits |
177 | 179 |
// |
178 | 180 |
// \brief Extender for maps which use a subset of the items. |
179 | 181 |
template <typename _Graph, typename _Map> |
180 | 182 |
class SubMapExtender : public _Map { |
181 | 183 |
typedef _Map Parent; |
182 | 184 |
typedef _Graph GraphType; |
183 | 185 |
|
184 | 186 |
public: |
185 | 187 |
|
186 | 188 |
typedef SubMapExtender Map; |
187 | 189 |
typedef typename Parent::Key Item; |
188 | 190 |
|
189 | 191 |
typedef typename Parent::Key Key; |
190 | 192 |
typedef typename Parent::Value Value; |
191 | 193 |
typedef typename Parent::Reference Reference; |
192 | 194 |
typedef typename Parent::ConstReference ConstReference; |
193 | 195 |
|
196 |
typedef typename Parent::ReferenceMapTag ReferenceMapTag; |
|
197 |
|
|
194 | 198 |
class MapIt; |
195 | 199 |
class ConstMapIt; |
196 | 200 |
|
197 | 201 |
friend class MapIt; |
198 | 202 |
friend class ConstMapIt; |
199 | 203 |
|
200 | 204 |
public: |
201 | 205 |
|
202 | 206 |
SubMapExtender(const GraphType& _graph) |
203 | 207 |
: Parent(_graph), graph(_graph) {} |
204 | 208 |
|
205 | 209 |
SubMapExtender(const GraphType& _graph, const Value& _value) |
206 | 210 |
: Parent(_graph, _value), graph(_graph) {} |
207 | 211 |
|
208 | 212 |
private: |
209 | 213 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
210 | 214 |
return operator=<MapExtender>(cmap); |
211 | 215 |
} |
212 | 216 |
|
213 | 217 |
template <typename CMap> |
214 | 218 |
SubMapExtender& operator=(const CMap& cmap) { |
215 | 219 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
216 | 220 |
Item it; |
217 | 221 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
218 | 222 |
Parent::set(it, cmap[it]); |
219 | 223 |
} |
220 | 224 |
return *this; |
221 | 225 |
} |
222 | 226 |
|
223 | 227 |
public: |
224 | 228 |
class MapIt : public Item { |
225 | 229 |
typedef Item Parent; |
226 | 230 |
|
227 | 231 |
public: |
228 | 232 |
typedef typename Map::Value Value; |
229 | 233 |
|
230 | 234 |
MapIt() {} |
231 | 235 |
|
232 | 236 |
MapIt(Invalid i) : Parent(i) { } |
233 | 237 |
|
234 | 238 |
explicit MapIt(Map& _map) : map(_map) { |
235 | 239 |
map.graph.first(*this); |
236 | 240 |
} |
237 | 241 |
|
238 | 242 |
MapIt(const Map& _map, const Item& item) |
239 | 243 |
: Parent(item), map(_map) {} |
240 | 244 |
|
241 | 245 |
MapIt& operator++() { |
242 | 246 |
map.graph.next(*this); |
243 | 247 |
return *this; |
244 | 248 |
} |
245 | 249 |
|
246 | 250 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
247 | 251 |
return map[*this]; |
248 | 252 |
} |
249 | 253 |
|
250 | 254 |
typename MapTraits<Map>::ReturnValue operator*() { |
251 | 255 |
return map[*this]; |
252 | 256 |
} |
253 | 257 |
|
254 | 258 |
void set(const Value& value) { |
255 | 259 |
map.set(*this, value); |
256 | 260 |
} |
257 | 261 |
|
258 | 262 |
protected: |
259 | 263 |
Map& map; |
260 | 264 |
|
261 | 265 |
}; |
262 | 266 |
|
263 | 267 |
class ConstMapIt : public Item { |
264 | 268 |
typedef Item Parent; |
265 | 269 |
|
266 | 270 |
public: |
267 | 271 |
|
268 | 272 |
typedef typename Map::Value Value; |
269 | 273 |
|
270 | 274 |
ConstMapIt() {} |
271 | 275 |
|
272 | 276 |
ConstMapIt(Invalid i) : Parent(i) { } |
273 | 277 |
|
274 | 278 |
explicit ConstMapIt(Map& _map) : map(_map) { |
275 | 279 |
map.graph.first(*this); |
276 | 280 |
} |
277 | 281 |
|
278 | 282 |
ConstMapIt(const Map& _map, const Item& item) |
279 | 283 |
: Parent(item), map(_map) {} |
280 | 284 |
|
281 | 285 |
ConstMapIt& operator++() { |
282 | 286 |
map.graph.next(*this); |
283 | 287 |
return *this; |
284 | 288 |
} |
285 | 289 |
|
286 | 290 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
287 | 291 |
return map[*this]; |
288 | 292 |
} |
289 | 293 |
|
290 | 294 |
protected: |
291 | 295 |
const Map& map; |
292 | 296 |
}; |
293 | 297 |
|
294 | 298 |
class ItemIt : public Item { |
295 | 299 |
typedef Item Parent; |
296 | 300 |
|
297 | 301 |
public: |
298 | 302 |
|
299 | 303 |
ItemIt() {} |
300 | 304 |
|
301 | 305 |
ItemIt(Invalid i) : Parent(i) { } |
302 | 306 |
|
303 | 307 |
explicit ItemIt(Map& _map) : map(_map) { |
304 | 308 |
map.graph.first(*this); |
305 | 309 |
} |
306 | 310 |
|
307 | 311 |
ItemIt(const Map& _map, const Item& item) |
308 | 312 |
: Parent(item), map(_map) {} |
309 | 313 |
|
310 | 314 |
ItemIt& operator++() { |
311 | 315 |
map.graph.next(*this); |
312 | 316 |
return *this; |
313 | 317 |
} |
314 | 318 |
|
315 | 319 |
protected: |
316 | 320 |
const Map& map; |
317 | 321 |
|
318 | 322 |
}; |
319 | 323 |
|
320 | 324 |
private: |
321 | 325 |
|
322 | 326 |
const GraphType& graph; |
323 | 327 |
|
324 | 328 |
}; |
325 | 329 |
|
326 | 330 |
} |
327 | 331 |
|
328 | 332 |
#endif |
1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
2 | 2 |
* |
3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
4 | 4 |
* |
5 | 5 |
* Copyright (C) 2003-2009 |
6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
8 | 8 |
* |
9 | 9 |
* Permission to use, modify and distribute this software is granted |
10 | 10 |
* provided that this copyright notice appears in all copies. For |
11 | 11 |
* precise terms see the accompanying LICENSE file. |
12 | 12 |
* |
13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
14 | 14 |
* express or implied, and with no claim as to its suitability for any |
15 | 15 |
* purpose. |
16 | 16 |
* |
17 | 17 |
*/ |
18 | 18 |
|
19 | 19 |
#ifndef LEMON_CONCEPTS_MAPS_H |
20 | 20 |
#define LEMON_CONCEPTS_MAPS_H |
21 | 21 |
|
22 | 22 |
#include <lemon/core.h> |
23 | 23 |
#include <lemon/concept_check.h> |
24 | 24 |
|
25 | 25 |
///\ingroup map_concepts |
26 | 26 |
///\file |
27 | 27 |
///\brief The concept of maps. |
28 | 28 |
|
29 | 29 |
namespace lemon { |
30 | 30 |
|
31 | 31 |
namespace concepts { |
32 | 32 |
|
33 | 33 |
/// \addtogroup map_concepts |
34 | 34 |
/// @{ |
35 | 35 |
|
36 | 36 |
/// Readable map concept |
37 | 37 |
|
38 | 38 |
/// Readable map concept. |
39 | 39 |
/// |
40 | 40 |
template<typename K, typename T> |
41 | 41 |
class ReadMap |
42 | 42 |
{ |
43 | 43 |
public: |
44 | 44 |
/// The key type of the map. |
45 | 45 |
typedef K Key; |
46 | 46 |
/// \brief The value type of the map. |
47 | 47 |
/// (The type of objects associated with the keys). |
48 | 48 |
typedef T Value; |
49 | 49 |
|
50 | 50 |
/// Returns the value associated with the given key. |
51 | 51 |
Value operator[](const Key &) const { |
52 | 52 |
return *static_cast<Value *>(0); |
53 | 53 |
} |
54 | 54 |
|
55 | 55 |
template<typename _ReadMap> |
56 | 56 |
struct Constraints { |
57 | 57 |
void constraints() { |
58 | 58 |
Value val = m[key]; |
59 | 59 |
val = m[key]; |
60 | 60 |
typename _ReadMap::Value own_val = m[own_key]; |
61 | 61 |
own_val = m[own_key]; |
62 | 62 |
|
63 | 63 |
ignore_unused_variable_warning(key); |
64 | 64 |
ignore_unused_variable_warning(val); |
65 | 65 |
ignore_unused_variable_warning(own_key); |
66 | 66 |
ignore_unused_variable_warning(own_val); |
67 | 67 |
} |
68 | 68 |
const Key& key; |
69 | 69 |
const typename _ReadMap::Key& own_key; |
70 | 70 |
const _ReadMap& m; |
71 | 71 |
}; |
72 | 72 |
|
73 | 73 |
}; |
74 | 74 |
|
75 | 75 |
|
76 | 76 |
/// Writable map concept |
77 | 77 |
|
78 | 78 |
/// Writable map concept. |
79 | 79 |
/// |
80 | 80 |
template<typename K, typename T> |
81 | 81 |
class WriteMap |
82 | 82 |
{ |
83 | 83 |
public: |
84 | 84 |
/// The key type of the map. |
85 | 85 |
typedef K Key; |
86 | 86 |
/// \brief The value type of the map. |
87 | 87 |
/// (The type of objects associated with the keys). |
88 | 88 |
typedef T Value; |
89 | 89 |
|
90 | 90 |
/// Sets the value associated with the given key. |
91 | 91 |
void set(const Key &, const Value &) {} |
92 | 92 |
|
93 | 93 |
/// Default constructor. |
94 | 94 |
WriteMap() {} |
95 | 95 |
|
96 | 96 |
template <typename _WriteMap> |
97 | 97 |
struct Constraints { |
98 | 98 |
void constraints() { |
99 | 99 |
m.set(key, val); |
100 | 100 |
m.set(own_key, own_val); |
101 | 101 |
|
102 | 102 |
ignore_unused_variable_warning(key); |
103 | 103 |
ignore_unused_variable_warning(val); |
104 | 104 |
ignore_unused_variable_warning(own_key); |
105 | 105 |
ignore_unused_variable_warning(own_val); |
106 | 106 |
} |
107 | 107 |
const Key& key; |
108 | 108 |
const Value& val; |
109 | 109 |
const typename _WriteMap::Key& own_key; |
110 | 110 |
const typename _WriteMap::Value& own_val; |
111 | 111 |
_WriteMap& m; |
112 | 112 |
}; |
113 | 113 |
}; |
114 | 114 |
|
115 | 115 |
/// Read/writable map concept |
116 | 116 |
|
117 | 117 |
/// Read/writable map concept. |
118 | 118 |
/// |
119 | 119 |
template<typename K, typename T> |
120 | 120 |
class ReadWriteMap : public ReadMap<K,T>, |
121 | 121 |
public WriteMap<K,T> |
122 | 122 |
{ |
123 | 123 |
public: |
124 | 124 |
/// The key type of the map. |
125 | 125 |
typedef K Key; |
126 | 126 |
/// \brief The value type of the map. |
127 | 127 |
/// (The type of objects associated with the keys). |
128 | 128 |
typedef T Value; |
129 | 129 |
|
130 | 130 |
/// Returns the value associated with the given key. |
131 | 131 |
Value operator[](const Key &) const { |
132 | 132 |
return *static_cast<Value *>(0); |
133 | 133 |
} |
134 | 134 |
|
135 | 135 |
/// Sets the value associated with the given key. |
136 | 136 |
void set(const Key &, const Value &) {} |
137 | 137 |
|
138 | 138 |
template<typename _ReadWriteMap> |
139 | 139 |
struct Constraints { |
140 | 140 |
void constraints() { |
141 | 141 |
checkConcept<ReadMap<K, T>, _ReadWriteMap >(); |
142 | 142 |
checkConcept<WriteMap<K, T>, _ReadWriteMap >(); |
143 | 143 |
} |
144 | 144 |
}; |
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
|
148 | 148 |
/// Dereferable map concept |
149 | 149 |
|
150 | 150 |
/// Dereferable map concept. |
151 | 151 |
/// |
152 | 152 |
template<typename K, typename T, typename R, typename CR> |
153 | 153 |
class ReferenceMap : public ReadWriteMap<K,T> |
154 | 154 |
{ |
155 | 155 |
public: |
156 | 156 |
/// Tag for reference maps. |
157 | 157 |
typedef True ReferenceMapTag; |
158 | 158 |
/// The key type of the map. |
159 | 159 |
typedef K Key; |
160 | 160 |
/// \brief The value type of the map. |
161 | 161 |
/// (The type of objects associated with the keys). |
162 | 162 |
typedef T Value; |
163 | 163 |
/// The reference type of the map. |
164 | 164 |
typedef R Reference; |
165 | 165 |
/// The const reference type of the map. |
166 | 166 |
typedef CR ConstReference; |
167 | 167 |
|
168 | 168 |
public: |
169 | 169 |
|
170 | 170 |
/// Returns a reference to the value associated with the given key. |
171 | 171 |
Reference operator[](const Key &) { |
172 | 172 |
return *static_cast<Value *>(0); |
173 | 173 |
} |
174 | 174 |
|
175 | 175 |
/// Returns a const reference to the value associated with the given key. |
176 | 176 |
ConstReference operator[](const Key &) const { |
177 | 177 |
return *static_cast<Value *>(0); |
178 | 178 |
} |
179 | 179 |
|
180 | 180 |
/// Sets the value associated with the given key. |
181 | 181 |
void set(const Key &k,const Value &t) { operator[](k)=t; } |
182 | 182 |
|
183 | 183 |
template<typename _ReferenceMap> |
184 | 184 |
struct Constraints { |
185 |
|
|
185 |
typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type |
|
186 |
constraints() { |
|
186 | 187 |
checkConcept<ReadWriteMap<K, T>, _ReferenceMap >(); |
187 | 188 |
ref = m[key]; |
188 | 189 |
m[key] = val; |
189 | 190 |
m[key] = ref; |
190 | 191 |
m[key] = cref; |
191 | 192 |
own_ref = m[own_key]; |
192 | 193 |
m[own_key] = own_val; |
193 | 194 |
m[own_key] = own_ref; |
194 | 195 |
m[own_key] = own_cref; |
195 | 196 |
m[key] = m[own_key]; |
196 | 197 |
m[own_key] = m[key]; |
197 | 198 |
} |
198 | 199 |
const Key& key; |
199 | 200 |
Value& val; |
200 | 201 |
Reference ref; |
201 | 202 |
ConstReference cref; |
202 | 203 |
const typename _ReferenceMap::Key& own_key; |
203 | 204 |
typename _ReferenceMap::Value& own_val; |
204 | 205 |
typename _ReferenceMap::Reference own_ref; |
205 | 206 |
typename _ReferenceMap::ConstReference own_cref; |
206 | 207 |
_ReferenceMap& m; |
207 | 208 |
}; |
208 | 209 |
}; |
209 | 210 |
|
210 | 211 |
// @} |
211 | 212 |
|
212 | 213 |
} //namespace concepts |
213 | 214 |
|
214 | 215 |
} //namespace lemon |
215 | 216 |
|
216 | 217 |
#endif |
0 comments (0 inline)