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-2008 |
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 |
public: |
40 | 40 |
|
41 | 41 |
typedef _Map Parent; |
42 | 42 |
typedef MapExtender Map; |
43 | 43 |
|
44 | 44 |
|
45 | 45 |
typedef typename Parent::Graph Graph; |
46 | 46 |
typedef typename Parent::Key Item; |
47 | 47 |
|
48 | 48 |
typedef typename Parent::Key Key; |
49 | 49 |
typedef typename Parent::Value Value; |
50 | 50 |
|
51 | 51 |
class MapIt; |
52 | 52 |
class ConstMapIt; |
53 | 53 |
|
54 | 54 |
friend class MapIt; |
55 | 55 |
friend class ConstMapIt; |
56 | 56 |
|
57 | 57 |
public: |
58 | 58 |
|
59 | 59 |
MapExtender(const Graph& graph) |
60 | 60 |
: Parent(graph) {} |
61 | 61 |
|
62 | 62 |
MapExtender(const Graph& graph, const Value& value) |
63 | 63 |
: Parent(graph, value) {} |
64 | 64 |
|
65 | 65 |
private: |
66 | 66 |
MapExtender& operator=(const MapExtender& cmap) { |
67 | 67 |
return operator=<MapExtender>(cmap); |
68 | 68 |
} |
69 | 69 |
|
70 | 70 |
template <typename CMap> |
71 | 71 |
MapExtender& operator=(const CMap& cmap) { |
72 | 72 |
Parent::operator=(cmap); |
73 | 73 |
return *this; |
74 | 74 |
} |
75 | 75 |
|
76 | 76 |
public: |
77 | 77 |
class MapIt : public Item { |
78 | 78 |
public: |
79 | 79 |
|
80 | 80 |
typedef Item Parent; |
81 | 81 |
typedef typename Map::Value Value; |
82 | 82 |
|
83 |
MapIt() {} |
|
83 |
MapIt() : map(NULL) {} |
|
84 | 84 |
|
85 |
MapIt(Invalid i) : Parent(i) { |
|
85 |
MapIt(Invalid i) : Parent(i), map(NULL) {} |
|
86 | 86 |
|
87 |
explicit MapIt(Map& _map) : map(_map) { |
|
88 |
map.notifier()->first(*this); |
|
87 |
explicit MapIt(Map& _map) : map(&_map) { |
|
88 |
map->notifier()->first(*this); |
|
89 | 89 |
} |
90 | 90 |
|
91 | 91 |
MapIt(const Map& _map, const Item& item) |
92 |
: Parent(item), map(_map) {} |
|
92 |
: Parent(item), map(&_map) {} |
|
93 | 93 |
|
94 | 94 |
MapIt& operator++() { |
95 |
map |
|
95 |
map->notifier()->next(*this); |
|
96 | 96 |
return *this; |
97 | 97 |
} |
98 | 98 |
|
99 | 99 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
100 |
return map[*this]; |
|
100 |
return (*map)[*this]; |
|
101 | 101 |
} |
102 | 102 |
|
103 | 103 |
typename MapTraits<Map>::ReturnValue operator*() { |
104 |
return map[*this]; |
|
104 |
return (*map)[*this]; |
|
105 | 105 |
} |
106 | 106 |
|
107 | 107 |
void set(const Value& value) { |
108 |
map |
|
108 |
map->set(*this, value); |
|
109 | 109 |
} |
110 | 110 |
|
111 | 111 |
protected: |
112 |
Map |
|
112 |
Map* map; |
|
113 | 113 |
|
114 | 114 |
}; |
115 | 115 |
|
116 | 116 |
class ConstMapIt : public Item { |
117 | 117 |
public: |
118 | 118 |
|
119 | 119 |
typedef Item Parent; |
120 | 120 |
|
121 | 121 |
typedef typename Map::Value Value; |
122 | 122 |
|
123 |
ConstMapIt() {} |
|
123 |
ConstMapIt() : map(NULL) {} |
|
124 | 124 |
|
125 |
ConstMapIt(Invalid i) : Parent(i) { |
|
125 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) {} |
|
126 | 126 |
|
127 |
explicit ConstMapIt(Map& _map) : map(_map) { |
|
128 |
map.notifier()->first(*this); |
|
127 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
|
128 |
map->notifier()->first(*this); |
|
129 | 129 |
} |
130 | 130 |
|
131 | 131 |
ConstMapIt(const Map& _map, const Item& item) |
132 | 132 |
: Parent(item), map(_map) {} |
133 | 133 |
|
134 | 134 |
ConstMapIt& operator++() { |
135 |
map |
|
135 |
map->notifier()->next(*this); |
|
136 | 136 |
return *this; |
137 | 137 |
} |
138 | 138 |
|
139 | 139 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
140 | 140 |
return map[*this]; |
141 | 141 |
} |
142 | 142 |
|
143 | 143 |
protected: |
144 |
const Map |
|
144 |
const Map* map; |
|
145 | 145 |
}; |
146 | 146 |
|
147 | 147 |
class ItemIt : public Item { |
148 | 148 |
public: |
149 | 149 |
|
150 | 150 |
typedef Item Parent; |
151 | 151 |
|
152 |
ItemIt() {} |
|
152 |
ItemIt() : map(NULL) {} |
|
153 | 153 |
|
154 |
ItemIt(Invalid i) : Parent(i) { |
|
154 |
ItemIt(Invalid i) : Parent(i), map(NULL) {} |
|
155 | 155 |
|
156 |
explicit ItemIt(Map& _map) : map(_map) { |
|
157 |
map.notifier()->first(*this); |
|
156 |
explicit ItemIt(Map& _map) : map(&_map) { |
|
157 |
map->notifier()->first(*this); |
|
158 | 158 |
} |
159 | 159 |
|
160 | 160 |
ItemIt(const Map& _map, const Item& item) |
161 |
: Parent(item), map(_map) {} |
|
161 |
: Parent(item), map(&_map) {} |
|
162 | 162 |
|
163 | 163 |
ItemIt& operator++() { |
164 |
map |
|
164 |
map->notifier()->next(*this); |
|
165 | 165 |
return *this; |
166 | 166 |
} |
167 | 167 |
|
168 | 168 |
protected: |
169 |
const Map |
|
169 |
const Map* map; |
|
170 | 170 |
|
171 | 171 |
}; |
172 | 172 |
}; |
173 | 173 |
|
174 | 174 |
// \ingroup graphbits |
175 | 175 |
// |
176 | 176 |
// \brief Extender for maps which use a subset of the items. |
177 | 177 |
template <typename _Graph, typename _Map> |
178 | 178 |
class SubMapExtender : public _Map { |
179 | 179 |
public: |
180 | 180 |
|
181 | 181 |
typedef _Map Parent; |
182 | 182 |
typedef SubMapExtender Map; |
183 | 183 |
|
184 | 184 |
typedef _Graph Graph; |
185 | 185 |
|
186 | 186 |
typedef typename Parent::Key Item; |
187 | 187 |
|
188 | 188 |
typedef typename Parent::Key Key; |
189 | 189 |
typedef typename Parent::Value Value; |
190 | 190 |
|
191 | 191 |
class MapIt; |
192 | 192 |
class ConstMapIt; |
193 | 193 |
|
194 | 194 |
friend class MapIt; |
195 | 195 |
friend class ConstMapIt; |
196 | 196 |
|
197 | 197 |
public: |
198 | 198 |
|
199 | 199 |
SubMapExtender(const Graph& _graph) |
200 | 200 |
: Parent(_graph), graph(_graph) {} |
201 | 201 |
|
202 | 202 |
SubMapExtender(const Graph& _graph, const Value& _value) |
203 | 203 |
: Parent(_graph, _value), graph(_graph) {} |
204 | 204 |
|
205 | 205 |
private: |
206 | 206 |
SubMapExtender& operator=(const SubMapExtender& cmap) { |
207 | 207 |
return operator=<MapExtender>(cmap); |
208 | 208 |
} |
209 | 209 |
|
210 | 210 |
template <typename CMap> |
211 | 211 |
SubMapExtender& operator=(const CMap& cmap) { |
212 | 212 |
checkConcept<concepts::ReadMap<Key, Value>, CMap>(); |
213 | 213 |
Item it; |
214 | 214 |
for (graph.first(it); it != INVALID; graph.next(it)) { |
215 | 215 |
Parent::set(it, cmap[it]); |
216 | 216 |
} |
217 | 217 |
return *this; |
218 | 218 |
} |
219 | 219 |
|
220 | 220 |
public: |
221 | 221 |
class MapIt : public Item { |
222 | 222 |
public: |
223 | 223 |
|
224 | 224 |
typedef Item Parent; |
225 | 225 |
typedef typename Map::Value Value; |
226 | 226 |
|
227 |
MapIt() {} |
|
227 |
MapIt() : map(NULL) {} |
|
228 | 228 |
|
229 |
MapIt(Invalid i) : Parent(i) { } |
|
229 |
MapIt(Invalid i) : Parent(i), map(NULL) { } |
|
230 | 230 |
|
231 |
explicit MapIt(Map& _map) : map(_map) { |
|
232 |
map.graph.first(*this); |
|
231 |
explicit MapIt(Map& _map) : map(&_map) { |
|
232 |
map->graph.first(*this); |
|
233 | 233 |
} |
234 | 234 |
|
235 | 235 |
MapIt(const Map& _map, const Item& item) |
236 |
: Parent(item), map(_map) {} |
|
236 |
: Parent(item), map(&_map) {} |
|
237 | 237 |
|
238 | 238 |
MapIt& operator++() { |
239 |
map |
|
239 |
map->graph.next(*this); |
|
240 | 240 |
return *this; |
241 | 241 |
} |
242 | 242 |
|
243 | 243 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
244 |
return map[*this]; |
|
244 |
return (*map)[*this]; |
|
245 | 245 |
} |
246 | 246 |
|
247 | 247 |
typename MapTraits<Map>::ReturnValue operator*() { |
248 |
return map[*this]; |
|
248 |
return (*map)[*this]; |
|
249 | 249 |
} |
250 | 250 |
|
251 | 251 |
void set(const Value& value) { |
252 |
map |
|
252 |
map->set(*this, value); |
|
253 | 253 |
} |
254 | 254 |
|
255 | 255 |
protected: |
256 |
Map |
|
256 |
Map* map; |
|
257 | 257 |
|
258 | 258 |
}; |
259 | 259 |
|
260 | 260 |
class ConstMapIt : public Item { |
261 | 261 |
public: |
262 | 262 |
|
263 | 263 |
typedef Item Parent; |
264 | 264 |
|
265 | 265 |
typedef typename Map::Value Value; |
266 | 266 |
|
267 |
ConstMapIt() {} |
|
267 |
ConstMapIt() : map(NULL) {} |
|
268 | 268 |
|
269 |
ConstMapIt(Invalid i) : Parent(i) { } |
|
269 |
ConstMapIt(Invalid i) : Parent(i), map(NULL) { } |
|
270 | 270 |
|
271 |
explicit ConstMapIt(Map& _map) : map(_map) { |
|
272 |
map.graph.first(*this); |
|
271 |
explicit ConstMapIt(Map& _map) : map(&_map) { |
|
272 |
map->graph.first(*this); |
|
273 | 273 |
} |
274 | 274 |
|
275 | 275 |
ConstMapIt(const Map& _map, const Item& item) |
276 |
: Parent(item), map(_map) {} |
|
276 |
: Parent(item), map(&_map) {} |
|
277 | 277 |
|
278 | 278 |
ConstMapIt& operator++() { |
279 |
map |
|
279 |
map->graph.next(*this); |
|
280 | 280 |
return *this; |
281 | 281 |
} |
282 | 282 |
|
283 | 283 |
typename MapTraits<Map>::ConstReturnValue operator*() const { |
284 |
return map[*this]; |
|
284 |
return (*map)[*this]; |
|
285 | 285 |
} |
286 | 286 |
|
287 | 287 |
protected: |
288 |
const Map |
|
288 |
const Map* map; |
|
289 | 289 |
}; |
290 | 290 |
|
291 | 291 |
class ItemIt : public Item { |
292 | 292 |
public: |
293 | 293 |
|
294 | 294 |
typedef Item Parent; |
295 | 295 |
|
296 |
ItemIt() {} |
|
296 |
ItemIt() : map(NULL) {} |
|
297 | 297 |
|
298 |
ItemIt(Invalid i) : Parent(i) { } |
|
298 |
ItemIt(Invalid i) : Parent(i), map(NULL) { } |
|
299 | 299 |
|
300 |
explicit ItemIt(Map& _map) : map(_map) { |
|
301 |
map.graph.first(*this); |
|
300 |
explicit ItemIt(Map& _map) : map(&_map) { |
|
301 |
map->graph.first(*this); |
|
302 | 302 |
} |
303 | 303 |
|
304 | 304 |
ItemIt(const Map& _map, const Item& item) |
305 |
: Parent(item), map(_map) {} |
|
305 |
: Parent(item), map(&_map) {} |
|
306 | 306 |
|
307 | 307 |
ItemIt& operator++() { |
308 |
map |
|
308 |
map->graph.next(*this); |
|
309 | 309 |
return *this; |
310 | 310 |
} |
311 | 311 |
|
312 | 312 |
protected: |
313 |
const Map |
|
313 |
const Map* map; |
|
314 | 314 |
|
315 | 315 |
}; |
316 | 316 |
|
317 | 317 |
private: |
318 | 318 |
|
319 | 319 |
const Graph& graph; |
320 | 320 |
|
321 | 321 |
}; |
322 | 322 |
|
323 | 323 |
} |
324 | 324 |
|
325 | 325 |
#endif |
0 comments (0 inline)