alpar@1677
|
1 |
/* -*- C++ -*-
|
alpar@1677
|
2 |
* lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
|
alpar@1677
|
3 |
*
|
alpar@1677
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1677
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@1677
|
6 |
*
|
alpar@1677
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@1677
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@1677
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@1677
|
10 |
*
|
alpar@1677
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@1677
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@1677
|
13 |
* purpose.
|
alpar@1677
|
14 |
*
|
alpar@1677
|
15 |
*/
|
alpar@1677
|
16 |
|
deba@1752
|
17 |
#include <lemon/traits.h>
|
deba@1752
|
18 |
#include <lemon/invalid.h>
|
alpar@1677
|
19 |
#include <vector>
|
alpar@1677
|
20 |
#include <limits>
|
alpar@1677
|
21 |
|
alpar@1677
|
22 |
///\ingroup maps
|
alpar@1677
|
23 |
///\file
|
alpar@1677
|
24 |
///\brief Maps that makes it possible to iterate through the keys having
|
alpar@1677
|
25 |
///a certain value
|
alpar@1677
|
26 |
///
|
alpar@1677
|
27 |
///
|
alpar@1677
|
28 |
|
alpar@1677
|
29 |
|
alpar@1677
|
30 |
namespace lemon {
|
alpar@1677
|
31 |
|
alpar@1677
|
32 |
///\todo This is only a static map!
|
alpar@1805
|
33 |
///\todo Undocumented.
|
alpar@1677
|
34 |
///\param BaseMap is an interger map.
|
alpar@1677
|
35 |
template<class BaseMap>
|
alpar@1677
|
36 |
class IterableBoolMap
|
alpar@1677
|
37 |
{
|
alpar@1677
|
38 |
public:
|
alpar@1677
|
39 |
|
alpar@1677
|
40 |
typedef typename BaseMap::Key Key;
|
alpar@1677
|
41 |
typedef bool Value;
|
alpar@1677
|
42 |
|
alpar@1677
|
43 |
friend class RefType;
|
alpar@1677
|
44 |
friend class FalseIt;
|
alpar@1677
|
45 |
friend class TrueIt;
|
alpar@1677
|
46 |
|
alpar@1677
|
47 |
private:
|
alpar@1677
|
48 |
BaseMap &cref;
|
alpar@1677
|
49 |
std::vector<Key> vals;
|
alpar@1677
|
50 |
int sep; //map[e] is true <=> cref[e]>=sep
|
alpar@1677
|
51 |
|
alpar@1677
|
52 |
bool isTrue(Key k) {return cref[k]>=sep;}
|
alpar@1677
|
53 |
void swap(Key k, int s)
|
alpar@1677
|
54 |
{
|
alpar@1677
|
55 |
int ti=cref[k];
|
alpar@1677
|
56 |
Key tk=vals[s];
|
alpar@1677
|
57 |
cref[k]=s; vals[s]=k;
|
alpar@1677
|
58 |
cref[tk]=ti; vals[ti]=tk;
|
alpar@1677
|
59 |
}
|
alpar@1677
|
60 |
|
alpar@1677
|
61 |
void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
|
alpar@1677
|
62 |
void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
|
alpar@1677
|
63 |
|
alpar@1677
|
64 |
public:
|
alpar@1677
|
65 |
///\e
|
alpar@1677
|
66 |
void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
|
alpar@1805
|
67 |
///Number of \c true items in the map
|
alpar@1805
|
68 |
|
alpar@1805
|
69 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
70 |
///This is a constant time operation.
|
alpar@1805
|
71 |
int countTrue() { return vals.size()-sep; }
|
alpar@1805
|
72 |
///Number of \c false items in the map
|
alpar@1805
|
73 |
|
alpar@1805
|
74 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
75 |
///This is a constant time operation.
|
alpar@1805
|
76 |
int countFalse() { return sep; }
|
alpar@1677
|
77 |
|
alpar@1677
|
78 |
///\e
|
alpar@1677
|
79 |
class FalseIt
|
alpar@1677
|
80 |
{
|
alpar@1677
|
81 |
const IterableBoolMap &M;
|
alpar@1677
|
82 |
int i;
|
alpar@1677
|
83 |
public:
|
alpar@1805
|
84 |
///\e
|
alpar@1677
|
85 |
explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
|
alpar@1805
|
86 |
///\e
|
alpar@1677
|
87 |
FalseIt(Invalid)
|
alpar@1677
|
88 |
: M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
|
alpar@1805
|
89 |
///\e
|
alpar@1677
|
90 |
FalseIt &operator++() { ++i; return *this;}
|
alpar@1805
|
91 |
///\e
|
alpar@1677
|
92 |
operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
|
alpar@1805
|
93 |
///\e
|
alpar@1677
|
94 |
bool operator !=(Invalid) const { return i<M.sep; }
|
alpar@1805
|
95 |
///\e
|
alpar@1677
|
96 |
bool operator ==(Invalid) const { return i>=M.sep; }
|
alpar@1677
|
97 |
};
|
alpar@1677
|
98 |
///\e
|
alpar@1677
|
99 |
class TrueIt
|
alpar@1677
|
100 |
{
|
alpar@1677
|
101 |
const IterableBoolMap &M;
|
alpar@1677
|
102 |
int i;
|
alpar@1677
|
103 |
public:
|
alpar@1805
|
104 |
///\e
|
alpar@1677
|
105 |
explicit TrueIt(const IterableBoolMap &_M)
|
alpar@1677
|
106 |
: M(_M), i(M.vals.size()-1) { }
|
alpar@1805
|
107 |
///\e
|
alpar@1677
|
108 |
TrueIt(Invalid)
|
alpar@1677
|
109 |
: M(*((IterableBoolMap*)(0))), i(-1) { }
|
alpar@1805
|
110 |
///\e
|
alpar@1677
|
111 |
TrueIt &operator++() { --i; return *this;}
|
alpar@1805
|
112 |
///\e
|
alpar@1677
|
113 |
operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
|
alpar@1805
|
114 |
///\e
|
alpar@1677
|
115 |
bool operator !=(Invalid) const { return i>=M.sep; }
|
alpar@1805
|
116 |
///\e
|
alpar@1677
|
117 |
bool operator ==(Invalid) const { return i<M.sep; }
|
alpar@1677
|
118 |
};
|
alpar@1677
|
119 |
|
alpar@1677
|
120 |
///\e
|
alpar@1677
|
121 |
class RefType
|
alpar@1677
|
122 |
{
|
alpar@1677
|
123 |
IterableBoolMap &M;
|
alpar@1677
|
124 |
Key k;
|
alpar@1677
|
125 |
public:
|
alpar@1677
|
126 |
RefType(IterableBoolMap &_M,Key _k) : M(_M), k(_k) { }
|
alpar@1677
|
127 |
|
alpar@1677
|
128 |
operator Value() const
|
alpar@1677
|
129 |
{
|
alpar@1677
|
130 |
return M.isTrue(k);
|
alpar@1677
|
131 |
}
|
alpar@1677
|
132 |
Value operator = (Value v) const { M.set(k,v); return v; }
|
alpar@1677
|
133 |
};
|
alpar@1677
|
134 |
|
alpar@1677
|
135 |
public:
|
alpar@1677
|
136 |
explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
|
alpar@1677
|
137 |
{
|
alpar@1677
|
138 |
sep=0;
|
deba@1810
|
139 |
for(typename BaseMap::MapIt i(cref);i!=INVALID; ++i) {
|
deba@1810
|
140 |
i.set(sep);
|
deba@1810
|
141 |
vals.push_back(i);
|
alpar@1677
|
142 |
sep++;
|
alpar@1677
|
143 |
}
|
alpar@1677
|
144 |
if(init) sep=0;
|
alpar@1677
|
145 |
}
|
alpar@1805
|
146 |
///\e
|
alpar@1677
|
147 |
RefType operator[] (Key k) { return RefType(*this,k);}
|
alpar@1805
|
148 |
///\e
|
alpar@1677
|
149 |
Value operator[] (Key k) const { return isTrue(k);}
|
alpar@1677
|
150 |
};
|
alpar@1677
|
151 |
|
alpar@1677
|
152 |
|
alpar@1677
|
153 |
|
alpar@1677
|
154 |
|
alpar@1677
|
155 |
/// \addtogroup graph_maps
|
alpar@1677
|
156 |
/// @{
|
alpar@1677
|
157 |
|
alpar@1677
|
158 |
/// Iterable bool NodeMap
|
alpar@1677
|
159 |
|
alpar@1677
|
160 |
/// This map can be used in the same way
|
alpar@1677
|
161 |
/// as the standard NodeMap<bool> of the
|
alpar@1677
|
162 |
/// given graph \c Graph.
|
alpar@1677
|
163 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1677
|
164 |
/// and \ref FalseIt to iterate through the "true" and "false" nodes.
|
alpar@1677
|
165 |
template <class Graph>
|
alpar@1677
|
166 |
class IterableBoolNodeMap
|
alpar@1677
|
167 |
{
|
deba@1685
|
168 |
typename Graph::template NodeMap<int> cmap;
|
alpar@1677
|
169 |
|
alpar@1677
|
170 |
public:
|
alpar@1677
|
171 |
|
deba@1685
|
172 |
typedef IterableBoolMap<typename Graph::template NodeMap<int> > BimType;
|
alpar@1677
|
173 |
BimType imap;
|
alpar@1677
|
174 |
|
alpar@1677
|
175 |
|
alpar@1677
|
176 |
typedef typename BimType::RefType RefType;
|
alpar@1677
|
177 |
typedef typename Graph::Node Key;
|
alpar@1677
|
178 |
typedef bool Value;
|
alpar@1677
|
179 |
|
alpar@1677
|
180 |
friend class FalseIt;
|
alpar@1677
|
181 |
friend class TrueIt;
|
alpar@1677
|
182 |
|
alpar@1677
|
183 |
///\e
|
alpar@1677
|
184 |
IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1677
|
185 |
|
alpar@1677
|
186 |
public:
|
alpar@1677
|
187 |
///\e
|
alpar@1677
|
188 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1805
|
189 |
///Number of \c true items in the map
|
alpar@1805
|
190 |
|
alpar@1805
|
191 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
192 |
///This is a constant time operation.
|
alpar@1805
|
193 |
int countTrue() { return imap.countTrue(); }
|
alpar@1805
|
194 |
///Number of \c false items in the map
|
alpar@1805
|
195 |
|
alpar@1805
|
196 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
197 |
///This is a constant time operation.
|
alpar@1805
|
198 |
int countFalse() { return imap.countFalse(); }
|
alpar@1677
|
199 |
#ifdef DOXYGEN
|
alpar@1677
|
200 |
///\e
|
alpar@1677
|
201 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1677
|
202 |
///\e
|
alpar@1677
|
203 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1677
|
204 |
#else
|
alpar@1677
|
205 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1677
|
206 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1677
|
207 |
#endif
|
alpar@1677
|
208 |
///Iterator for the "false" nodes
|
alpar@1677
|
209 |
class FalseIt : public BimType::FalseIt
|
alpar@1677
|
210 |
{
|
alpar@1677
|
211 |
public:
|
alpar@1805
|
212 |
///\e
|
alpar@1677
|
213 |
explicit FalseIt(const IterableBoolNodeMap &m)
|
alpar@1677
|
214 |
: BimType::FalseIt(m.imap) { }
|
alpar@1805
|
215 |
///\e
|
alpar@1677
|
216 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1677
|
217 |
};
|
alpar@1677
|
218 |
///Iterator for the "true" nodes
|
alpar@1677
|
219 |
class TrueIt : public BimType::TrueIt
|
alpar@1677
|
220 |
{
|
alpar@1677
|
221 |
public:
|
alpar@1805
|
222 |
///\e
|
alpar@1677
|
223 |
explicit TrueIt(const IterableBoolNodeMap &m)
|
alpar@1677
|
224 |
: BimType::TrueIt(m.imap) { }
|
alpar@1805
|
225 |
///\e
|
alpar@1677
|
226 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1677
|
227 |
};
|
alpar@1677
|
228 |
};
|
alpar@1677
|
229 |
|
alpar@1677
|
230 |
/// Iterable bool EdgeMap
|
alpar@1677
|
231 |
|
alpar@1677
|
232 |
/// This map can be used in the same way
|
alpar@1677
|
233 |
/// as the standard EdgeMap<bool> of the
|
alpar@1677
|
234 |
/// given graph \c Graph.
|
alpar@1677
|
235 |
/// In addition, this class provides two iterators called \ref TrueIt
|
alpar@1677
|
236 |
/// and \ref FalseIt to iterate through the "true" and "false" edges.
|
alpar@1677
|
237 |
template <class Graph>
|
alpar@1677
|
238 |
class IterableBoolEdgeMap
|
alpar@1677
|
239 |
{
|
deba@1685
|
240 |
typename Graph::template EdgeMap<int> cmap;
|
alpar@1677
|
241 |
|
alpar@1677
|
242 |
public:
|
alpar@1677
|
243 |
|
deba@1685
|
244 |
typedef IterableBoolMap<typename Graph::template EdgeMap<int> > BimType;
|
alpar@1677
|
245 |
BimType imap;
|
alpar@1677
|
246 |
|
alpar@1677
|
247 |
|
alpar@1677
|
248 |
typedef typename BimType::RefType RefType;
|
alpar@1677
|
249 |
typedef typename Graph::Edge Key;
|
alpar@1677
|
250 |
typedef bool Value;
|
alpar@1677
|
251 |
|
alpar@1677
|
252 |
friend class FalseIt;
|
alpar@1677
|
253 |
friend class TrueIt;
|
alpar@1677
|
254 |
|
alpar@1677
|
255 |
///\e
|
alpar@1677
|
256 |
IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
|
alpar@1677
|
257 |
|
alpar@1677
|
258 |
public:
|
alpar@1677
|
259 |
///\e
|
alpar@1677
|
260 |
void set(Key k, bool v) { imap.set(k,v);}
|
alpar@1805
|
261 |
///Returns the number of \c true values in the map.
|
alpar@1805
|
262 |
///This is a constant time operation.
|
alpar@1805
|
263 |
int countTrue() { return imap.countTrue(); }
|
alpar@1805
|
264 |
///Number of \c false items in the map
|
alpar@1805
|
265 |
|
alpar@1805
|
266 |
///Returns the number of \c false values in the map.
|
alpar@1805
|
267 |
///This is a constant time operation.
|
alpar@1805
|
268 |
int countFalse() { return imap.countFalse(); }
|
alpar@1677
|
269 |
#ifdef DOXYGEN
|
alpar@1677
|
270 |
///\e
|
alpar@1677
|
271 |
bool &operator[](Key k) { return imap[k];}
|
alpar@1677
|
272 |
///\e
|
alpar@1677
|
273 |
const bool &operator[](Key k) const { return imap[k];}
|
alpar@1677
|
274 |
#else
|
alpar@1677
|
275 |
Value operator[](Key k) const { return imap[k];}
|
alpar@1677
|
276 |
RefType operator[](Key k) { return imap[k];}
|
alpar@1677
|
277 |
#endif
|
alpar@1677
|
278 |
///Iterator for the "false" edges
|
alpar@1677
|
279 |
class FalseIt : public BimType::FalseIt
|
alpar@1677
|
280 |
{
|
alpar@1677
|
281 |
public:
|
alpar@1805
|
282 |
///\e
|
alpar@1677
|
283 |
explicit FalseIt(const IterableBoolEdgeMap &m)
|
alpar@1677
|
284 |
: BimType::FalseIt(m.imap) { }
|
alpar@1805
|
285 |
///\e
|
alpar@1677
|
286 |
FalseIt(Invalid i) : BimType::FalseIt(i) { }
|
alpar@1677
|
287 |
};
|
alpar@1677
|
288 |
///Iterator for the "true" edges
|
alpar@1677
|
289 |
class TrueIt : public BimType::TrueIt
|
alpar@1677
|
290 |
{
|
alpar@1677
|
291 |
public:
|
alpar@1805
|
292 |
///\e
|
alpar@1677
|
293 |
explicit TrueIt(const IterableBoolEdgeMap &m)
|
alpar@1677
|
294 |
: BimType::TrueIt(m.imap) { }
|
alpar@1805
|
295 |
///\e
|
alpar@1677
|
296 |
TrueIt(Invalid i) : BimType::TrueIt(i) { }
|
alpar@1677
|
297 |
};
|
alpar@1677
|
298 |
};
|
alpar@1677
|
299 |
|
deba@1752
|
300 |
|
deba@1752
|
301 |
namespace _iterable_maps_bits {
|
deba@1752
|
302 |
template <typename Item>
|
deba@1752
|
303 |
struct IterableIntMapNode {
|
deba@1810
|
304 |
IterableIntMapNode() {}
|
deba@1810
|
305 |
IterableIntMapNode(int _value) : value(_value) {}
|
deba@1752
|
306 |
Item prev, next;
|
deba@1752
|
307 |
int value;
|
deba@1752
|
308 |
};
|
deba@1752
|
309 |
}
|
deba@1752
|
310 |
|
deba@1752
|
311 |
///\ingroup maps
|
deba@1752
|
312 |
///
|
deba@1752
|
313 |
/// \brief Dynamic iterable integer map.
|
deba@1752
|
314 |
///
|
deba@1810
|
315 |
/// This class provides a special graph map type which can store
|
deba@1810
|
316 |
/// for each graph item(node, edge, etc.) an integer value. For each
|
deba@1810
|
317 |
/// non negative value it is possible to iterate on the keys which
|
deba@1810
|
318 |
/// mapped to the given value.
|
deba@1810
|
319 |
///
|
deba@1810
|
320 |
/// \param _Graph The graph type.
|
deba@1810
|
321 |
/// \param _Item One of the graph's item type, the key of the map.
|
deba@1752
|
322 |
template <typename _Graph, typename _Item>
|
deba@1752
|
323 |
class IterableIntMap : protected ItemSetTraits<_Graph, _Item>
|
deba@1752
|
324 |
::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
|
deba@1752
|
325 |
public:
|
deba@1752
|
326 |
typedef typename ItemSetTraits<_Graph, _Item>
|
deba@1752
|
327 |
::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
|
deba@1752
|
328 |
::Parent Parent;
|
deba@1752
|
329 |
|
deba@1810
|
330 |
/// The key type
|
deba@1752
|
331 |
typedef _Item Key;
|
deba@1810
|
332 |
/// The value type
|
deba@1752
|
333 |
typedef int Value;
|
deba@1810
|
334 |
/// The graph type
|
deba@1752
|
335 |
typedef _Graph Graph;
|
deba@1752
|
336 |
|
deba@1810
|
337 |
/// \brief Constructor of the Map.
|
deba@1810
|
338 |
///
|
deba@1810
|
339 |
/// Constructor of the Map. It set all values -1.
|
deba@1810
|
340 |
explicit IterableIntMap(const Graph& graph)
|
deba@1810
|
341 |
: Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(-1)) {}
|
deba@1810
|
342 |
|
deba@1810
|
343 |
/// \brief Constructor of the Map with a given value.
|
deba@1810
|
344 |
///
|
deba@1810
|
345 |
/// Constructor of the Map with a given value.
|
deba@1810
|
346 |
explicit IterableIntMap(const Graph& graph, int value)
|
deba@1810
|
347 |
: Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
|
deba@1810
|
348 |
if (value >= 0) {
|
deba@1810
|
349 |
for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
|
deba@1810
|
350 |
lace(it);
|
deba@1810
|
351 |
}
|
deba@1810
|
352 |
}
|
deba@1810
|
353 |
}
|
deba@1752
|
354 |
|
deba@1752
|
355 |
private:
|
deba@1752
|
356 |
|
deba@1752
|
357 |
void unlace(const Key& key) {
|
deba@1752
|
358 |
typename Parent::Value& node = Parent::operator[](key);
|
deba@1752
|
359 |
if (node.value < 0) return;
|
deba@1752
|
360 |
if (node.prev != INVALID) {
|
deba@1752
|
361 |
Parent::operator[](node.prev).next = node.next;
|
deba@1752
|
362 |
} else {
|
deba@1752
|
363 |
first[node.value] = node.next;
|
deba@1752
|
364 |
}
|
deba@1752
|
365 |
if (node.next != INVALID) {
|
deba@1752
|
366 |
Parent::operator[](node.next).prev = node.prev;
|
deba@1752
|
367 |
}
|
deba@1752
|
368 |
while (!first.empty() && first.back() == INVALID) {
|
deba@1752
|
369 |
first.pop_back();
|
deba@1752
|
370 |
}
|
deba@1752
|
371 |
}
|
deba@1752
|
372 |
|
deba@1752
|
373 |
void lace(const Key& key) {
|
deba@1752
|
374 |
typename Parent::Value& node = Parent::operator[](key);
|
deba@1752
|
375 |
if (node.value < 0) return;
|
deba@1752
|
376 |
if (node.value >= (int)first.size()) {
|
deba@1752
|
377 |
first.resize(node.value + 1, INVALID);
|
deba@1752
|
378 |
}
|
deba@1752
|
379 |
node.prev = INVALID;
|
deba@1752
|
380 |
node.next = first[node.value];
|
deba@1752
|
381 |
if (node.next != INVALID) {
|
deba@1752
|
382 |
Parent::operator[](node.next).prev = key;
|
deba@1752
|
383 |
}
|
deba@1752
|
384 |
first[node.value] = key;
|
deba@1752
|
385 |
}
|
deba@1752
|
386 |
|
deba@1752
|
387 |
public:
|
deba@1752
|
388 |
|
deba@1810
|
389 |
/// Indicates that the map if reference map.
|
deba@1752
|
390 |
typedef True ReferenceMapTag;
|
deba@1752
|
391 |
|
deba@1810
|
392 |
/// \brief Refernce to the value of the map.
|
deba@1810
|
393 |
///
|
deba@1810
|
394 |
/// This class is near to similar to the int type. It can
|
deba@1810
|
395 |
/// be converted to int and it has the same operators.
|
deba@1752
|
396 |
class Reference {
|
deba@1752
|
397 |
friend class IterableIntMap;
|
deba@1752
|
398 |
private:
|
deba@1752
|
399 |
Reference(IterableIntMap& map, const Key& key)
|
deba@1752
|
400 |
: _key(key), _map(map) {}
|
deba@1752
|
401 |
public:
|
deba@1752
|
402 |
|
deba@1752
|
403 |
Reference& operator=(const Reference& value) {
|
deba@1752
|
404 |
_map.set(_key, (const int&)value);
|
deba@1752
|
405 |
return *this;
|
deba@1752
|
406 |
}
|
deba@1752
|
407 |
|
deba@1752
|
408 |
operator const int&() const {
|
deba@1752
|
409 |
return static_cast<const IterableIntMap&>(_map)[_key];
|
deba@1752
|
410 |
}
|
deba@1752
|
411 |
|
deba@1752
|
412 |
Reference& operator=(int value) {
|
deba@1752
|
413 |
_map.set(_key, value);
|
deba@1752
|
414 |
return *this;
|
deba@1752
|
415 |
}
|
deba@1759
|
416 |
Reference& operator++() {
|
deba@1759
|
417 |
_map.set(_key, _map[_key] + 1);
|
deba@1759
|
418 |
return *this;
|
deba@1759
|
419 |
}
|
deba@1759
|
420 |
int operator++(int) {
|
deba@1759
|
421 |
int value = _map[_key];
|
deba@1759
|
422 |
_map.set(_key, value + 1);
|
deba@1759
|
423 |
return value;
|
deba@1759
|
424 |
}
|
deba@1759
|
425 |
Reference& operator--() {
|
deba@1759
|
426 |
_map.set(_key, _map[_key] - 1);
|
deba@1759
|
427 |
return *this;
|
deba@1759
|
428 |
}
|
deba@1759
|
429 |
int operator--(int) {
|
deba@1759
|
430 |
int value = _map[_key];
|
deba@1759
|
431 |
_map.set(_key, value - 1);
|
deba@1759
|
432 |
return value;
|
deba@1759
|
433 |
}
|
deba@1752
|
434 |
Reference& operator+=(int value) {
|
deba@1752
|
435 |
_map.set(_key, _map[_key] + value);
|
deba@1752
|
436 |
return *this;
|
deba@1752
|
437 |
}
|
deba@1752
|
438 |
Reference& operator-=(int value) {
|
deba@1752
|
439 |
_map.set(_key, _map[_key] - value);
|
deba@1752
|
440 |
return *this;
|
deba@1752
|
441 |
}
|
deba@1752
|
442 |
Reference& operator*=(int value) {
|
deba@1752
|
443 |
_map.set(_key, _map[_key] * value);
|
deba@1752
|
444 |
return *this;
|
deba@1752
|
445 |
}
|
deba@1752
|
446 |
Reference& operator/=(int value) {
|
deba@1752
|
447 |
_map.set(_key, _map[_key] / value);
|
deba@1752
|
448 |
return *this;
|
deba@1752
|
449 |
}
|
deba@1752
|
450 |
Reference& operator%=(int value) {
|
deba@1752
|
451 |
_map.set(_key, _map[_key] % value);
|
deba@1752
|
452 |
return *this;
|
deba@1752
|
453 |
}
|
deba@1752
|
454 |
Reference& operator&=(int value) {
|
deba@1752
|
455 |
_map.set(_key, _map[_key] & value);
|
deba@1752
|
456 |
return *this;
|
deba@1752
|
457 |
}
|
deba@1752
|
458 |
Reference& operator|=(int value) {
|
deba@1752
|
459 |
_map.set(_key, _map[_key] | value);
|
deba@1752
|
460 |
return *this;
|
deba@1752
|
461 |
}
|
deba@1752
|
462 |
Reference& operator^=(int value) {
|
deba@1752
|
463 |
_map.set(_key, _map[_key] ^ value);
|
deba@1752
|
464 |
return *this;
|
deba@1752
|
465 |
}
|
deba@1752
|
466 |
Reference& operator<<=(int value) {
|
deba@1752
|
467 |
_map.set(_key, _map[_key] << value);
|
deba@1752
|
468 |
return *this;
|
deba@1752
|
469 |
}
|
deba@1752
|
470 |
Reference& operator>>=(int value) {
|
deba@1752
|
471 |
_map.set(_key, _map[_key] >> value);
|
deba@1752
|
472 |
return *this;
|
deba@1752
|
473 |
}
|
deba@1752
|
474 |
|
deba@1752
|
475 |
private:
|
deba@1752
|
476 |
Key _key;
|
deba@1752
|
477 |
IterableIntMap& _map;
|
deba@1752
|
478 |
};
|
deba@1810
|
479 |
|
deba@1810
|
480 |
/// The const reference type.
|
deba@1752
|
481 |
typedef const Value& ConstReference;
|
deba@1752
|
482 |
|
deba@1810
|
483 |
/// \brief Gives back the maximal value plus one.
|
deba@1810
|
484 |
///
|
deba@1810
|
485 |
/// Gives back the maximal value plus one.
|
deba@1752
|
486 |
int size() const {
|
deba@1752
|
487 |
return (int)first.size();
|
deba@1752
|
488 |
}
|
deba@1752
|
489 |
|
deba@1810
|
490 |
/// \brief Set operation of the map.
|
deba@1810
|
491 |
///
|
deba@1810
|
492 |
/// Set operation of the map.
|
deba@1752
|
493 |
void set(const Key& key, const Value& value) {
|
deba@1752
|
494 |
unlace(key);
|
deba@1752
|
495 |
Parent::operator[](key).value = value;
|
deba@1752
|
496 |
lace(key);
|
deba@1752
|
497 |
}
|
deba@1752
|
498 |
|
deba@1810
|
499 |
/// \brief Const subscript operator of the map.
|
deba@1810
|
500 |
///
|
deba@1810
|
501 |
/// Const subscript operator of the map.
|
deba@1752
|
502 |
const Value& operator[](const Key& key) const {
|
deba@1752
|
503 |
return Parent::operator[](key).value;
|
deba@1752
|
504 |
}
|
deba@1752
|
505 |
|
deba@1810
|
506 |
/// \brief Subscript operator of the map.
|
deba@1810
|
507 |
///
|
deba@1810
|
508 |
/// Subscript operator of the map.
|
deba@1752
|
509 |
Reference operator[](const Key& key) {
|
deba@1752
|
510 |
return Reference(*this, key);
|
deba@1752
|
511 |
}
|
deba@1752
|
512 |
|
deba@1810
|
513 |
/// \brief Iterator for the keys with the same value.
|
deba@1810
|
514 |
///
|
deba@1810
|
515 |
/// Iterator for the keys with the same value. It works
|
deba@1810
|
516 |
/// like a graph item iterator in the map, it can be converted
|
deba@1810
|
517 |
/// the item type of the map, incremented with \c ++ operator, and
|
deba@1810
|
518 |
/// if the iterator leave the last valid item it will be equal to
|
deba@1810
|
519 |
/// \c INVALID.
|
deba@1752
|
520 |
class ItemIt : public _Item {
|
deba@1752
|
521 |
public:
|
deba@1752
|
522 |
typedef _Item Parent;
|
deba@1752
|
523 |
|
deba@1810
|
524 |
/// \brief Invalid constructor \& conversion.
|
deba@1810
|
525 |
///
|
deba@1810
|
526 |
/// This constructor initializes the item to be invalid.
|
deba@1810
|
527 |
/// \sa Invalid for more details.
|
deba@1752
|
528 |
ItemIt(Invalid) : Parent(INVALID), _map(0) {}
|
deba@1752
|
529 |
|
deba@1810
|
530 |
/// \brief Creates an iterator with a value.
|
deba@1810
|
531 |
///
|
deba@1810
|
532 |
/// Creates an iterator with a value. It iterates on the
|
deba@1810
|
533 |
/// keys which have the given value.
|
deba@1810
|
534 |
/// \param map The IterableIntMap
|
deba@1810
|
535 |
/// \param value The value
|
deba@1752
|
536 |
ItemIt(const IterableIntMap& map, int value) : _map(&map) {
|
deba@1752
|
537 |
if (value < 0 || value >= (int)_map->first.size()) {
|
deba@1752
|
538 |
Parent::operator=(INVALID);
|
deba@1752
|
539 |
} else {
|
deba@1752
|
540 |
Parent::operator=(_map->first[value]);
|
deba@1752
|
541 |
}
|
deba@1752
|
542 |
}
|
deba@1752
|
543 |
|
deba@1810
|
544 |
/// \brief Increment operator.
|
deba@1810
|
545 |
///
|
deba@1810
|
546 |
/// Increment Operator.
|
deba@1752
|
547 |
ItemIt& operator++() {
|
deba@1752
|
548 |
Parent::operator=(_map->IterableIntMap::Parent::
|
deba@1752
|
549 |
operator[](static_cast<Parent&>(*this)).next);
|
deba@1752
|
550 |
return *this;
|
deba@1752
|
551 |
}
|
deba@1752
|
552 |
|
deba@1752
|
553 |
|
deba@1752
|
554 |
private:
|
deba@1752
|
555 |
const IterableIntMap* _map;
|
deba@1752
|
556 |
};
|
deba@1752
|
557 |
|
deba@1752
|
558 |
protected:
|
deba@1752
|
559 |
|
deba@1752
|
560 |
virtual void erase(const Key& key) {
|
deba@1752
|
561 |
unlace(key);
|
deba@1752
|
562 |
Parent::erase(key);
|
deba@1752
|
563 |
}
|
deba@1752
|
564 |
|
deba@1752
|
565 |
virtual void clear() {
|
deba@1752
|
566 |
first.clear();
|
deba@1752
|
567 |
Parent::clear();
|
deba@1752
|
568 |
}
|
deba@1752
|
569 |
|
deba@1752
|
570 |
private:
|
deba@1752
|
571 |
std::vector<_Item> first;
|
deba@1752
|
572 |
};
|
deba@1752
|
573 |
|
alpar@1677
|
574 |
/// @}
|
alpar@1677
|
575 |
}
|