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