author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#ifndef QMAP_H |
|
43 |
#define QMAP_H |
|
44 |
||
45 |
#include <QtCore/qatomic.h> |
|
46 |
#include <QtCore/qiterator.h> |
|
47 |
#include <QtCore/qlist.h> |
|
48 |
||
49 |
#ifndef QT_NO_STL |
|
50 |
#include <map> |
|
51 |
#endif |
|
52 |
||
53 |
#include <new> |
|
54 |
||
55 |
QT_BEGIN_HEADER |
|
56 |
||
57 |
QT_BEGIN_NAMESPACE |
|
58 |
||
59 |
QT_MODULE(Core) |
|
60 |
||
61 |
struct Q_CORE_EXPORT QMapData |
|
62 |
{ |
|
63 |
struct Node { |
|
64 |
Node *backward; |
|
65 |
Node *forward[1]; |
|
66 |
}; |
|
67 |
enum { LastLevel = 11, Sparseness = 3 }; |
|
68 |
||
69 |
QMapData *backward; |
|
70 |
QMapData *forward[QMapData::LastLevel + 1]; |
|
71 |
QBasicAtomicInt ref; |
|
72 |
int topLevel; |
|
73 |
int size; |
|
74 |
uint randomBits; |
|
75 |
uint insertInOrder : 1; |
|
76 |
uint sharable : 1; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
77 |
uint strictAlignment : 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
78 |
uint reserved : 29; |
0 | 79 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
80 |
static QMapData *createData(); // ### Qt5 remove me |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
static QMapData *createData(int alignment); |
0 | 82 |
void continueFreeData(int offset); |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
Node *node_create(Node *update[], int offset); // ### Qt5 remove me |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
Node *node_create(Node *update[], int offset, int alignment); |
0 | 85 |
void node_delete(Node *update[], int offset, Node *node); |
86 |
#ifdef QT_QMAP_DEBUG |
|
87 |
uint adjust_ptr(Node *node); |
|
88 |
void dump(); |
|
89 |
#endif |
|
90 |
||
91 |
static QMapData shared_null; |
|
92 |
}; |
|
93 |
||
94 |
||
95 |
/* |
|
96 |
QMap uses qMapLessThanKey() to compare keys. The default |
|
97 |
implementation uses operator<(). For pointer types, |
|
98 |
qMapLessThanKey() casts the pointers to integers before it |
|
99 |
compares them, because operator<() is undefined on pointers |
|
100 |
that come from different memory blocks. (In practice, this |
|
101 |
is only a problem when running a program such as |
|
102 |
BoundsChecker.) |
|
103 |
*/ |
|
104 |
||
105 |
template <class Key> inline bool qMapLessThanKey(const Key &key1, const Key &key2) |
|
106 |
{ |
|
107 |
return key1 < key2; |
|
108 |
} |
|
109 |
||
110 |
#ifndef QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION |
|
111 |
template <class Ptr> inline bool qMapLessThanKey(Ptr *key1, Ptr *key2) |
|
112 |
{ |
|
113 |
Q_ASSERT(sizeof(quintptr) == sizeof(Ptr *)); |
|
114 |
return quintptr(key1) < quintptr(key2); |
|
115 |
} |
|
116 |
||
117 |
template <class Ptr> inline bool qMapLessThanKey(const Ptr *key1, const Ptr *key2) |
|
118 |
{ |
|
119 |
Q_ASSERT(sizeof(quintptr) == sizeof(const Ptr *)); |
|
120 |
return quintptr(key1) < quintptr(key2); |
|
121 |
} |
|
122 |
#endif // QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION |
|
123 |
||
124 |
template <class Key, class T> |
|
125 |
struct QMapNode { |
|
126 |
Key key; |
|
127 |
T value; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
128 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
129 |
private: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
130 |
// never access these members through this structure. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
131 |
// see below |
0 | 132 |
QMapData::Node *backward; |
133 |
QMapData::Node *forward[1]; |
|
134 |
}; |
|
135 |
||
136 |
template <class Key, class T> |
|
137 |
struct QMapPayloadNode |
|
138 |
{ |
|
139 |
Key key; |
|
140 |
T value; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
141 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
142 |
private: |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
143 |
// QMap::e is a pointer to QMapData::Node, which matches the member |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
144 |
// below. However, the memory allocation node in QMapData::node_create |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
145 |
// allocates sizeof(QMapPayloNode) and incorrectly calculates the offset |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
146 |
// of 'backward' below. If the alignment of QMapPayloadNode is larger |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
147 |
// than the alignment of a pointer, the 'backward' member is aligned to |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
148 |
// the end of this structure, not to 'value' above, and will occupy the |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
149 |
// tail-padding area. |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
150 |
// |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
151 |
// e.g., on a 32-bit archictecture with Key = int and |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
152 |
// sizeof(T) = alignof(T) = 8 |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
153 |
// 0 4 8 12 16 20 24 byte |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
154 |
// | key | PAD | value |backward| PAD | correct layout |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
155 |
// | key | PAD | value | |backward| how it's actually used |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
156 |
// |<----- value of QMap::payload() = 20 ----->| |
0 | 157 |
QMapData::Node *backward; |
158 |
}; |
|
159 |
||
160 |
template <class Key, class T> |
|
161 |
class QMap |
|
162 |
{ |
|
163 |
typedef QMapNode<Key, T> Node; |
|
164 |
typedef QMapPayloadNode<Key, T> PayloadNode; |
|
165 |
||
166 |
union { |
|
167 |
QMapData *d; |
|
168 |
QMapData::Node *e; |
|
169 |
}; |
|
170 |
||
171 |
static inline int payload() { return sizeof(PayloadNode) - sizeof(QMapData::Node *); } |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
static inline int alignment() { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
#ifdef Q_ALIGNOF |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
return int(qMax(sizeof(void*), Q_ALIGNOF(Node))); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
return 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
} |
0 | 179 |
static inline Node *concrete(QMapData::Node *node) { |
180 |
return reinterpret_cast<Node *>(reinterpret_cast<char *>(node) - payload()); |
|
181 |
} |
|
182 |
||
183 |
public: |
|
184 |
inline QMap() : d(&QMapData::shared_null) { d->ref.ref(); } |
|
185 |
inline QMap(const QMap<Key, T> &other) : d(other.d) |
|
186 |
{ d->ref.ref(); if (!d->sharable) detach(); } |
|
187 |
inline ~QMap() { if (!d) return; if (!d->ref.deref()) freeData(d); } |
|
188 |
||
189 |
QMap<Key, T> &operator=(const QMap<Key, T> &other); |
|
190 |
#ifndef QT_NO_STL |
|
191 |
explicit QMap(const typename std::map<Key, T> &other); |
|
192 |
std::map<Key, T> toStdMap() const; |
|
193 |
#endif |
|
194 |
||
195 |
bool operator==(const QMap<Key, T> &other) const; |
|
196 |
inline bool operator!=(const QMap<Key, T> &other) const { return !(*this == other); } |
|
197 |
||
198 |
inline int size() const { return d->size; } |
|
199 |
||
200 |
inline bool isEmpty() const { return d->size == 0; } |
|
201 |
||
202 |
inline void detach() { if (d->ref != 1) detach_helper(); } |
|
203 |
inline bool isDetached() const { return d->ref == 1; } |
|
204 |
inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; } |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
205 |
inline bool isSharedWith(const QMap<Key, T> &other) const { return d == other.d; } |
0 | 206 |
inline void setInsertInOrder(bool ordered) { d->insertInOrder = ordered; } |
207 |
||
208 |
void clear(); |
|
209 |
||
210 |
int remove(const Key &key); |
|
211 |
T take(const Key &key); |
|
212 |
||
213 |
bool contains(const Key &key) const; |
|
214 |
const Key key(const T &value) const; |
|
215 |
const Key key(const T &value, const Key &defaultKey) const; |
|
216 |
const T value(const Key &key) const; |
|
217 |
const T value(const Key &key, const T &defaultValue) const; |
|
218 |
T &operator[](const Key &key); |
|
219 |
const T operator[](const Key &key) const; |
|
220 |
||
221 |
QList<Key> uniqueKeys() const; |
|
222 |
QList<Key> keys() const; |
|
223 |
QList<Key> keys(const T &value) const; |
|
224 |
QList<T> values() const; |
|
225 |
QList<T> values(const Key &key) const; |
|
226 |
int count(const Key &key) const; |
|
227 |
||
228 |
class const_iterator; |
|
229 |
||
230 |
class iterator |
|
231 |
{ |
|
232 |
friend class const_iterator; |
|
233 |
QMapData::Node *i; |
|
234 |
||
235 |
public: |
|
236 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
237 |
typedef qptrdiff difference_type; |
0 | 238 |
typedef T value_type; |
239 |
typedef T *pointer; |
|
240 |
typedef T &reference; |
|
241 |
||
242 |
// ### Qt 5: get rid of 'operator Node *' |
|
243 |
inline operator QMapData::Node *() const { return i; } |
|
244 |
inline iterator() : i(0) { } |
|
245 |
inline iterator(QMapData::Node *node) : i(node) { } |
|
246 |
||
247 |
inline const Key &key() const { return concrete(i)->key; } |
|
248 |
inline T &value() const { return concrete(i)->value; } |
|
249 |
#ifdef QT3_SUPPORT |
|
250 |
inline QT3_SUPPORT T &data() const { return concrete(i)->value; } |
|
251 |
#endif |
|
252 |
inline T &operator*() const { return concrete(i)->value; } |
|
253 |
inline T *operator->() const { return &concrete(i)->value; } |
|
254 |
inline bool operator==(const iterator &o) const { return i == o.i; } |
|
255 |
inline bool operator!=(const iterator &o) const { return i != o.i; } |
|
256 |
||
257 |
inline iterator &operator++() { |
|
258 |
i = i->forward[0]; |
|
259 |
return *this; |
|
260 |
} |
|
261 |
inline iterator operator++(int) { |
|
262 |
iterator r = *this; |
|
263 |
i = i->forward[0]; |
|
264 |
return r; |
|
265 |
} |
|
266 |
inline iterator &operator--() { |
|
267 |
i = i->backward; |
|
268 |
return *this; |
|
269 |
} |
|
270 |
inline iterator operator--(int) { |
|
271 |
iterator r = *this; |
|
272 |
i = i->backward; |
|
273 |
return r; |
|
274 |
} |
|
275 |
inline iterator operator+(int j) const |
|
276 |
{ iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } |
|
277 |
inline iterator operator-(int j) const { return operator+(-j); } |
|
278 |
inline iterator &operator+=(int j) { return *this = *this + j; } |
|
279 |
inline iterator &operator-=(int j) { return *this = *this - j; } |
|
280 |
||
281 |
// ### Qt 5: not sure this is necessary anymore |
|
282 |
#ifdef QT_STRICT_ITERATORS |
|
283 |
private: |
|
284 |
#else |
|
285 |
public: |
|
286 |
#endif |
|
287 |
inline bool operator==(const const_iterator &o) const |
|
288 |
{ return i == o.i; } |
|
289 |
inline bool operator!=(const const_iterator &o) const |
|
290 |
{ return i != o.i; } |
|
291 |
||
292 |
private: |
|
293 |
// ### Qt 5: remove |
|
294 |
inline operator bool() const { return false; } |
|
295 |
}; |
|
296 |
friend class iterator; |
|
297 |
||
298 |
class const_iterator |
|
299 |
{ |
|
300 |
friend class iterator; |
|
301 |
QMapData::Node *i; |
|
302 |
||
303 |
public: |
|
304 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
305 |
typedef qptrdiff difference_type; |
0 | 306 |
typedef T value_type; |
307 |
typedef const T *pointer; |
|
308 |
typedef const T &reference; |
|
309 |
||
310 |
// ### Qt 5: get rid of 'operator Node *' |
|
311 |
inline operator QMapData::Node *() const { return i; } |
|
312 |
inline const_iterator() : i(0) { } |
|
313 |
inline const_iterator(QMapData::Node *node) : i(node) { } |
|
314 |
#ifdef QT_STRICT_ITERATORS |
|
315 |
explicit inline const_iterator(const iterator &o) |
|
316 |
#else |
|
317 |
inline const_iterator(const iterator &o) |
|
318 |
#endif |
|
319 |
{ i = o.i; } |
|
320 |
||
321 |
inline const Key &key() const { return concrete(i)->key; } |
|
322 |
inline const T &value() const { return concrete(i)->value; } |
|
323 |
#ifdef QT3_SUPPORT |
|
324 |
inline QT3_SUPPORT const T &data() const { return concrete(i)->value; } |
|
325 |
#endif |
|
326 |
inline const T &operator*() const { return concrete(i)->value; } |
|
327 |
inline const T *operator->() const { return &concrete(i)->value; } |
|
328 |
inline bool operator==(const const_iterator &o) const { return i == o.i; } |
|
329 |
inline bool operator!=(const const_iterator &o) const { return i != o.i; } |
|
330 |
||
331 |
inline const_iterator &operator++() { |
|
332 |
i = i->forward[0]; |
|
333 |
return *this; |
|
334 |
} |
|
335 |
inline const_iterator operator++(int) { |
|
336 |
const_iterator r = *this; |
|
337 |
i = i->forward[0]; |
|
338 |
return r; |
|
339 |
} |
|
340 |
inline const_iterator &operator--() { |
|
341 |
i = i->backward; |
|
342 |
return *this; |
|
343 |
} |
|
344 |
inline const_iterator operator--(int) { |
|
345 |
const_iterator r = *this; |
|
346 |
i = i->backward; |
|
347 |
return r; |
|
348 |
} |
|
349 |
inline const_iterator operator+(int j) const |
|
350 |
{ const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } |
|
351 |
inline const_iterator operator-(int j) const { return operator+(-j); } |
|
352 |
inline const_iterator &operator+=(int j) { return *this = *this + j; } |
|
353 |
inline const_iterator &operator-=(int j) { return *this = *this - j; } |
|
354 |
||
355 |
// ### Qt 5: not sure this is necessary anymore |
|
356 |
#ifdef QT_STRICT_ITERATORS |
|
357 |
private: |
|
358 |
inline bool operator==(const iterator &o) { return operator==(const_iterator(o)); } |
|
359 |
inline bool operator!=(const iterator &o) { return operator!=(const_iterator(o)); } |
|
360 |
#endif |
|
361 |
||
362 |
private: |
|
363 |
// ### Qt 5: remove |
|
364 |
inline operator bool() const { return false; } |
|
365 |
}; |
|
366 |
friend class const_iterator; |
|
367 |
||
368 |
// STL style |
|
369 |
inline iterator begin() { detach(); return iterator(e->forward[0]); } |
|
370 |
inline const_iterator begin() const { return const_iterator(e->forward[0]); } |
|
371 |
inline const_iterator constBegin() const { return const_iterator(e->forward[0]); } |
|
372 |
inline iterator end() { |
|
373 |
detach(); |
|
374 |
return iterator(e); |
|
375 |
} |
|
376 |
inline const_iterator end() const { return const_iterator(e); } |
|
377 |
inline const_iterator constEnd() const { return const_iterator(e); } |
|
378 |
iterator erase(iterator it); |
|
379 |
#ifdef QT3_SUPPORT |
|
380 |
inline QT3_SUPPORT iterator remove(iterator it) { return erase(it); } |
|
381 |
inline QT3_SUPPORT void erase(const Key &aKey) { remove(aKey); } |
|
382 |
#endif |
|
383 |
||
384 |
// more Qt |
|
385 |
typedef iterator Iterator; |
|
386 |
typedef const_iterator ConstIterator; |
|
387 |
inline int count() const { return d->size; } |
|
388 |
iterator find(const Key &key); |
|
389 |
const_iterator find(const Key &key) const; |
|
390 |
const_iterator constFind(const Key &key) const; |
|
391 |
iterator lowerBound(const Key &key); |
|
392 |
const_iterator lowerBound(const Key &key) const; |
|
393 |
iterator upperBound(const Key &key); |
|
394 |
const_iterator upperBound(const Key &key) const; |
|
395 |
iterator insert(const Key &key, const T &value); |
|
396 |
#ifdef QT3_SUPPORT |
|
397 |
QT3_SUPPORT iterator insert(const Key &key, const T &value, bool overwrite); |
|
398 |
#endif |
|
399 |
iterator insertMulti(const Key &key, const T &value); |
|
400 |
#ifdef QT3_SUPPORT |
|
401 |
inline QT3_SUPPORT iterator replace(const Key &aKey, const T &aValue) { return insert(aKey, aValue); } |
|
402 |
#endif |
|
403 |
QMap<Key, T> &unite(const QMap<Key, T> &other); |
|
404 |
||
405 |
// STL compatibility |
|
406 |
typedef Key key_type; |
|
407 |
typedef T mapped_type; |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
408 |
typedef qptrdiff difference_type; |
0 | 409 |
typedef int size_type; |
410 |
inline bool empty() const { return isEmpty(); } |
|
411 |
||
412 |
#ifdef QT_QMAP_DEBUG |
|
413 |
inline void dump() const { d->dump(); } |
|
414 |
#endif |
|
415 |
||
416 |
private: |
|
417 |
void detach_helper(); |
|
418 |
void freeData(QMapData *d); |
|
419 |
QMapData::Node *findNode(const Key &key) const; |
|
420 |
QMapData::Node *mutableFindNode(QMapData::Node *update[], const Key &key) const; |
|
421 |
QMapData::Node *node_create(QMapData *d, QMapData::Node *update[], const Key &key, |
|
422 |
const T &value); |
|
423 |
}; |
|
424 |
||
425 |
template <class Key, class T> |
|
426 |
Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::operator=(const QMap<Key, T> &other) |
|
427 |
{ |
|
428 |
if (d != other.d) { |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
429 |
QMapData* o = other.d; |
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
430 |
o->ref.ref(); |
0 | 431 |
if (!d->ref.deref()) |
432 |
freeData(d); |
|
37
758a864f9613
Revision: 201037
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
33
diff
changeset
|
433 |
d = o; |
0 | 434 |
if (!d->sharable) |
435 |
detach_helper(); |
|
436 |
} |
|
437 |
return *this; |
|
438 |
} |
|
439 |
||
440 |
template <class Key, class T> |
|
441 |
Q_INLINE_TEMPLATE void QMap<Key, T>::clear() |
|
442 |
{ |
|
443 |
*this = QMap<Key, T>(); |
|
444 |
} |
|
445 |
||
446 |
template <class Key, class T> |
|
447 |
Q_INLINE_TEMPLATE typename QMapData::Node * |
|
448 |
QMap<Key, T>::node_create(QMapData *adt, QMapData::Node *aupdate[], const Key &akey, const T &avalue) |
|
449 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
450 |
QMapData::Node *abstractNode = adt->node_create(aupdate, payload(), alignment()); |
0 | 451 |
QT_TRY { |
452 |
Node *concreteNode = concrete(abstractNode); |
|
453 |
new (&concreteNode->key) Key(akey); |
|
454 |
QT_TRY { |
|
455 |
new (&concreteNode->value) T(avalue); |
|
456 |
} QT_CATCH(...) { |
|
457 |
concreteNode->key.~Key(); |
|
458 |
QT_RETHROW; |
|
459 |
} |
|
460 |
} QT_CATCH(...) { |
|
461 |
adt->node_delete(aupdate, payload(), abstractNode); |
|
462 |
QT_RETHROW; |
|
463 |
} |
|
464 |
||
465 |
// clean up the update array for further insertions |
|
466 |
/* |
|
467 |
for (int i = 0; i <= d->topLevel; ++i) { |
|
468 |
if ( aupdate[i]==reinterpret_cast<QMapData::Node *>(adt) || aupdate[i]->forward[i] != abstractNode) |
|
469 |
break; |
|
470 |
aupdate[i] = abstractNode; |
|
471 |
} |
|
472 |
*/ |
|
473 |
||
474 |
return abstractNode; |
|
475 |
} |
|
476 |
||
477 |
template <class Key, class T> |
|
478 |
Q_INLINE_TEMPLATE QMapData::Node *QMap<Key, T>::findNode(const Key &akey) const |
|
479 |
{ |
|
480 |
QMapData::Node *cur = e; |
|
481 |
QMapData::Node *next = e; |
|
482 |
||
483 |
for (int i = d->topLevel; i >= 0; i--) { |
|
484 |
while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) |
|
485 |
cur = next; |
|
486 |
} |
|
487 |
||
488 |
if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { |
|
489 |
return next; |
|
490 |
} else { |
|
491 |
return e; |
|
492 |
} |
|
493 |
} |
|
494 |
||
495 |
template <class Key, class T> |
|
496 |
Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey) const |
|
497 |
{ |
|
498 |
QMapData::Node *node; |
|
499 |
if (d->size == 0 || (node = findNode(akey)) == e) { |
|
500 |
return T(); |
|
501 |
} else { |
|
502 |
return concrete(node)->value; |
|
503 |
} |
|
504 |
} |
|
505 |
||
506 |
template <class Key, class T> |
|
507 |
Q_INLINE_TEMPLATE const T QMap<Key, T>::value(const Key &akey, const T &adefaultValue) const |
|
508 |
{ |
|
509 |
QMapData::Node *node; |
|
510 |
if (d->size == 0 || (node = findNode(akey)) == e) { |
|
511 |
return adefaultValue; |
|
512 |
} else { |
|
513 |
return concrete(node)->value; |
|
514 |
} |
|
515 |
} |
|
516 |
||
517 |
template <class Key, class T> |
|
518 |
Q_INLINE_TEMPLATE const T QMap<Key, T>::operator[](const Key &akey) const |
|
519 |
{ |
|
520 |
return value(akey); |
|
521 |
} |
|
522 |
||
523 |
template <class Key, class T> |
|
524 |
Q_INLINE_TEMPLATE T &QMap<Key, T>::operator[](const Key &akey) |
|
525 |
{ |
|
526 |
detach(); |
|
527 |
||
528 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
529 |
QMapData::Node *node = mutableFindNode(update, akey); |
|
530 |
if (node == e) |
|
531 |
node = node_create(d, update, akey, T()); |
|
532 |
return concrete(node)->value; |
|
533 |
} |
|
534 |
||
535 |
template <class Key, class T> |
|
536 |
Q_INLINE_TEMPLATE int QMap<Key, T>::count(const Key &akey) const |
|
537 |
{ |
|
538 |
int cnt = 0; |
|
539 |
QMapData::Node *node = findNode(akey); |
|
540 |
if (node != e) { |
|
541 |
do { |
|
542 |
++cnt; |
|
543 |
node = node->forward[0]; |
|
544 |
} while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)); |
|
545 |
} |
|
546 |
return cnt; |
|
547 |
} |
|
548 |
||
549 |
template <class Key, class T> |
|
550 |
Q_INLINE_TEMPLATE bool QMap<Key, T>::contains(const Key &akey) const |
|
551 |
{ |
|
552 |
return findNode(akey) != e; |
|
553 |
} |
|
554 |
||
555 |
template <class Key, class T> |
|
556 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, |
|
557 |
const T &avalue) |
|
558 |
{ |
|
559 |
detach(); |
|
560 |
||
561 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
562 |
QMapData::Node *node = mutableFindNode(update, akey); |
|
563 |
if (node == e) { |
|
564 |
node = node_create(d, update, akey, avalue); |
|
565 |
} else { |
|
566 |
concrete(node)->value = avalue; |
|
567 |
} |
|
568 |
return iterator(node); |
|
569 |
} |
|
570 |
||
571 |
#ifdef QT3_SUPPORT |
|
572 |
template <class Key, class T> |
|
573 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insert(const Key &akey, |
|
574 |
const T &avalue, |
|
575 |
bool aoverwrite) |
|
576 |
{ |
|
577 |
detach(); |
|
578 |
||
579 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
580 |
QMapData::Node *node = mutableFindNode(update, akey); |
|
581 |
if (node == e) { |
|
582 |
node = node_create(d, update, akey, avalue); |
|
583 |
} else { |
|
584 |
if (aoverwrite) |
|
585 |
concrete(node)->value = avalue; |
|
586 |
} |
|
587 |
return iterator(node); |
|
588 |
} |
|
589 |
#endif |
|
590 |
||
591 |
template <class Key, class T> |
|
592 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::insertMulti(const Key &akey, |
|
593 |
const T &avalue) |
|
594 |
{ |
|
595 |
detach(); |
|
596 |
||
597 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
598 |
mutableFindNode(update, akey); |
|
599 |
return iterator(node_create(d, update, akey, avalue)); |
|
600 |
} |
|
601 |
||
602 |
template <class Key, class T> |
|
603 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::find(const Key &akey) const |
|
604 |
{ |
|
605 |
return const_iterator(findNode(akey)); |
|
606 |
} |
|
607 |
||
608 |
template <class Key, class T> |
|
609 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator QMap<Key, T>::constFind(const Key &akey) const |
|
610 |
{ |
|
611 |
return const_iterator(findNode(akey)); |
|
612 |
} |
|
613 |
||
614 |
template <class Key, class T> |
|
615 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::find(const Key &akey) |
|
616 |
{ |
|
617 |
detach(); |
|
618 |
return iterator(findNode(akey)); |
|
619 |
} |
|
620 |
||
621 |
template <class Key, class T> |
|
622 |
Q_INLINE_TEMPLATE QMap<Key, T> &QMap<Key, T>::unite(const QMap<Key, T> &other) |
|
623 |
{ |
|
624 |
QMap<Key, T> copy(other); |
|
625 |
const_iterator it = copy.constEnd(); |
|
626 |
const const_iterator b = copy.constBegin(); |
|
627 |
while (it != b) { |
|
628 |
--it; |
|
629 |
insertMulti(it.key(), it.value()); |
|
630 |
} |
|
631 |
return *this; |
|
632 |
} |
|
633 |
||
634 |
template <class Key, class T> |
|
635 |
Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::freeData(QMapData *x) |
|
636 |
{ |
|
637 |
if (QTypeInfo<Key>::isComplex || QTypeInfo<T>::isComplex) { |
|
638 |
QMapData *cur = x; |
|
639 |
QMapData *next = cur->forward[0]; |
|
640 |
while (next != x) { |
|
641 |
cur = next; |
|
642 |
next = cur->forward[0]; |
|
643 |
#if defined(_MSC_VER) && (_MSC_VER >= 1300) |
|
644 |
#pragma warning(disable:4189) |
|
645 |
#endif |
|
646 |
Node *concreteNode = concrete(reinterpret_cast<QMapData::Node *>(cur)); |
|
647 |
concreteNode->key.~Key(); |
|
648 |
concreteNode->value.~T(); |
|
649 |
#if defined(_MSC_VER) && (_MSC_VER >= 1300) |
|
650 |
#pragma warning(default:4189) |
|
651 |
#endif |
|
652 |
} |
|
653 |
} |
|
654 |
x->continueFreeData(payload()); |
|
655 |
} |
|
656 |
||
657 |
template <class Key, class T> |
|
658 |
Q_OUTOFLINE_TEMPLATE int QMap<Key, T>::remove(const Key &akey) |
|
659 |
{ |
|
660 |
detach(); |
|
661 |
||
662 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
663 |
QMapData::Node *cur = e; |
|
664 |
QMapData::Node *next = e; |
|
665 |
int oldSize = d->size; |
|
666 |
||
667 |
for (int i = d->topLevel; i >= 0; i--) { |
|
668 |
while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) |
|
669 |
cur = next; |
|
670 |
update[i] = cur; |
|
671 |
} |
|
672 |
||
673 |
if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { |
|
674 |
bool deleteNext = true; |
|
675 |
do { |
|
676 |
cur = next; |
|
677 |
next = cur->forward[0]; |
|
678 |
deleteNext = (next != e && !qMapLessThanKey<Key>(concrete(cur)->key, concrete(next)->key)); |
|
679 |
concrete(cur)->key.~Key(); |
|
680 |
concrete(cur)->value.~T(); |
|
681 |
d->node_delete(update, payload(), cur); |
|
682 |
} while (deleteNext); |
|
683 |
} |
|
684 |
return oldSize - d->size; |
|
685 |
} |
|
686 |
||
687 |
template <class Key, class T> |
|
688 |
Q_OUTOFLINE_TEMPLATE T QMap<Key, T>::take(const Key &akey) |
|
689 |
{ |
|
690 |
detach(); |
|
691 |
||
692 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
693 |
QMapData::Node *cur = e; |
|
694 |
QMapData::Node *next = e; |
|
695 |
||
696 |
for (int i = d->topLevel; i >= 0; i--) { |
|
697 |
while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) |
|
698 |
cur = next; |
|
699 |
update[i] = cur; |
|
700 |
} |
|
701 |
||
702 |
if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { |
|
703 |
T t = concrete(next)->value; |
|
704 |
concrete(next)->key.~Key(); |
|
705 |
concrete(next)->value.~T(); |
|
706 |
d->node_delete(update, payload(), next); |
|
707 |
return t; |
|
708 |
} |
|
709 |
return T(); |
|
710 |
} |
|
711 |
||
712 |
template <class Key, class T> |
|
713 |
Q_OUTOFLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::erase(iterator it) |
|
714 |
{ |
|
715 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
716 |
QMapData::Node *cur = e; |
|
717 |
QMapData::Node *next = e; |
|
718 |
||
719 |
if (it == iterator(e)) |
|
720 |
return it; |
|
721 |
||
722 |
for (int i = d->topLevel; i >= 0; i--) { |
|
723 |
while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, it.key())) |
|
724 |
cur = next; |
|
725 |
update[i] = cur; |
|
726 |
} |
|
727 |
||
728 |
while (next != e) { |
|
729 |
cur = next; |
|
730 |
next = cur->forward[0]; |
|
731 |
if (cur == it) { |
|
732 |
concrete(cur)->key.~Key(); |
|
733 |
concrete(cur)->value.~T(); |
|
734 |
d->node_delete(update, payload(), cur); |
|
735 |
return iterator(next); |
|
736 |
} |
|
737 |
||
738 |
for (int i = 0; i <= d->topLevel; ++i) { |
|
739 |
if (update[i]->forward[i] != cur) |
|
740 |
break; |
|
741 |
update[i] = cur; |
|
742 |
} |
|
743 |
} |
|
744 |
return end(); |
|
745 |
} |
|
746 |
||
747 |
template <class Key, class T> |
|
748 |
Q_OUTOFLINE_TEMPLATE void QMap<Key, T>::detach_helper() |
|
749 |
{ |
|
750 |
union { QMapData *d; QMapData::Node *e; } x; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
751 |
x.d = QMapData::createData(alignment()); |
0 | 752 |
if (d->size) { |
753 |
x.d->insertInOrder = true; |
|
754 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
755 |
QMapData::Node *cur = e->forward[0]; |
|
756 |
update[0] = x.e; |
|
757 |
while (cur != e) { |
|
758 |
QT_TRY { |
|
759 |
Node *concreteNode = concrete(cur); |
|
760 |
node_create(x.d, update, concreteNode->key, concreteNode->value); |
|
761 |
} QT_CATCH(...) { |
|
762 |
freeData(x.d); |
|
763 |
QT_RETHROW; |
|
764 |
} |
|
765 |
cur = cur->forward[0]; |
|
766 |
} |
|
767 |
x.d->insertInOrder = false; |
|
768 |
} |
|
769 |
if (!d->ref.deref()) |
|
770 |
freeData(d); |
|
771 |
d = x.d; |
|
772 |
} |
|
773 |
||
774 |
template <class Key, class T> |
|
775 |
Q_OUTOFLINE_TEMPLATE QMapData::Node *QMap<Key, T>::mutableFindNode(QMapData::Node *aupdate[], |
|
776 |
const Key &akey) const |
|
777 |
{ |
|
778 |
QMapData::Node *cur = e; |
|
779 |
QMapData::Node *next = e; |
|
780 |
||
781 |
for (int i = d->topLevel; i >= 0; i--) { |
|
782 |
while ((next = cur->forward[i]) != e && qMapLessThanKey<Key>(concrete(next)->key, akey)) |
|
783 |
cur = next; |
|
784 |
aupdate[i] = cur; |
|
785 |
} |
|
786 |
if (next != e && !qMapLessThanKey<Key>(akey, concrete(next)->key)) { |
|
787 |
return next; |
|
788 |
} else { |
|
789 |
return e; |
|
790 |
} |
|
791 |
} |
|
792 |
||
793 |
template <class Key, class T> |
|
794 |
Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::uniqueKeys() const |
|
795 |
{ |
|
796 |
QList<Key> res; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
797 |
res.reserve(size()); // May be too much, but assume short lifetime |
0 | 798 |
const_iterator i = begin(); |
799 |
if (i != end()) { |
|
800 |
for (;;) { |
|
801 |
const Key &aKey = i.key(); |
|
802 |
res.append(aKey); |
|
803 |
do { |
|
804 |
if (++i == end()) |
|
805 |
goto break_out_of_outer_loop; |
|
806 |
} while (!(aKey < i.key())); // loop while (key == i.key()) |
|
807 |
} |
|
808 |
} |
|
809 |
break_out_of_outer_loop: |
|
810 |
return res; |
|
811 |
} |
|
812 |
||
813 |
template <class Key, class T> |
|
814 |
Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys() const |
|
815 |
{ |
|
816 |
QList<Key> res; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
817 |
res.reserve(size()); |
0 | 818 |
const_iterator i = begin(); |
819 |
while (i != end()) { |
|
820 |
res.append(i.key()); |
|
821 |
++i; |
|
822 |
} |
|
823 |
return res; |
|
824 |
} |
|
825 |
||
826 |
template <class Key, class T> |
|
827 |
Q_OUTOFLINE_TEMPLATE QList<Key> QMap<Key, T>::keys(const T &avalue) const |
|
828 |
{ |
|
829 |
QList<Key> res; |
|
830 |
const_iterator i = begin(); |
|
831 |
while (i != end()) { |
|
832 |
if (i.value() == avalue) |
|
833 |
res.append(i.key()); |
|
834 |
++i; |
|
835 |
} |
|
836 |
return res; |
|
837 |
} |
|
838 |
||
839 |
template <class Key, class T> |
|
840 |
Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue) const |
|
841 |
{ |
|
842 |
return key(avalue, Key()); |
|
843 |
} |
|
844 |
||
845 |
template <class Key, class T> |
|
846 |
Q_OUTOFLINE_TEMPLATE const Key QMap<Key, T>::key(const T &avalue, const Key &defaultKey) const |
|
847 |
{ |
|
848 |
const_iterator i = begin(); |
|
849 |
while (i != end()) { |
|
850 |
if (i.value() == avalue) |
|
851 |
return i.key(); |
|
852 |
++i; |
|
853 |
} |
|
854 |
||
855 |
return defaultKey; |
|
856 |
} |
|
857 |
||
858 |
template <class Key, class T> |
|
859 |
Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values() const |
|
860 |
{ |
|
861 |
QList<T> res; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
862 |
res.reserve(size()); |
0 | 863 |
const_iterator i = begin(); |
864 |
while (i != end()) { |
|
865 |
res.append(i.value()); |
|
866 |
++i; |
|
867 |
} |
|
868 |
return res; |
|
869 |
} |
|
870 |
||
871 |
template <class Key, class T> |
|
872 |
Q_OUTOFLINE_TEMPLATE QList<T> QMap<Key, T>::values(const Key &akey) const |
|
873 |
{ |
|
874 |
QList<T> res; |
|
875 |
QMapData::Node *node = findNode(akey); |
|
876 |
if (node != e) { |
|
877 |
do { |
|
878 |
res.append(concrete(node)->value); |
|
879 |
node = node->forward[0]; |
|
880 |
} while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)); |
|
881 |
} |
|
882 |
return res; |
|
883 |
} |
|
884 |
||
885 |
template <class Key, class T> |
|
886 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator |
|
887 |
QMap<Key, T>::lowerBound(const Key &akey) const |
|
888 |
{ |
|
889 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
890 |
mutableFindNode(update, akey); |
|
891 |
return const_iterator(update[0]->forward[0]); |
|
892 |
} |
|
893 |
||
894 |
template <class Key, class T> |
|
895 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::lowerBound(const Key &akey) |
|
896 |
{ |
|
897 |
detach(); |
|
898 |
return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->lowerBound(akey)); |
|
899 |
} |
|
900 |
||
901 |
template <class Key, class T> |
|
902 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::const_iterator |
|
903 |
QMap<Key, T>::upperBound(const Key &akey) const |
|
904 |
{ |
|
905 |
QMapData::Node *update[QMapData::LastLevel + 1]; |
|
906 |
mutableFindNode(update, akey); |
|
907 |
QMapData::Node *node = update[0]->forward[0]; |
|
908 |
while (node != e && !qMapLessThanKey<Key>(akey, concrete(node)->key)) |
|
909 |
node = node->forward[0]; |
|
910 |
return const_iterator(node); |
|
911 |
} |
|
912 |
||
913 |
template <class Key, class T> |
|
914 |
Q_INLINE_TEMPLATE typename QMap<Key, T>::iterator QMap<Key, T>::upperBound(const Key &akey) |
|
915 |
{ |
|
916 |
detach(); |
|
917 |
return static_cast<QMapData::Node *>(const_cast<const QMap *>(this)->upperBound(akey)); |
|
918 |
} |
|
919 |
||
920 |
template <class Key, class T> |
|
921 |
Q_OUTOFLINE_TEMPLATE bool QMap<Key, T>::operator==(const QMap<Key, T> &other) const |
|
922 |
{ |
|
923 |
if (size() != other.size()) |
|
924 |
return false; |
|
925 |
if (d == other.d) |
|
926 |
return true; |
|
927 |
||
928 |
const_iterator it1 = begin(); |
|
929 |
const_iterator it2 = other.begin(); |
|
930 |
||
931 |
while (it1 != end()) { |
|
932 |
if (!(it1.value() == it2.value()) || qMapLessThanKey(it1.key(), it2.key()) || qMapLessThanKey(it2.key(), it1.key())) |
|
933 |
return false; |
|
934 |
++it2; |
|
935 |
++it1; |
|
936 |
} |
|
937 |
return true; |
|
938 |
} |
|
939 |
||
940 |
#ifndef QT_NO_STL |
|
941 |
template <class Key, class T> |
|
942 |
Q_OUTOFLINE_TEMPLATE QMap<Key, T>::QMap(const std::map<Key, T> &other) |
|
943 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
944 |
d = QMapData::createData(alignment()); |
0 | 945 |
d->insertInOrder = true; |
946 |
typename std::map<Key,T>::const_iterator it = other.end(); |
|
947 |
while (it != other.begin()) { |
|
948 |
--it; |
|
949 |
insert((*it).first, (*it).second); |
|
950 |
} |
|
951 |
d->insertInOrder = false; |
|
952 |
} |
|
953 |
||
954 |
template <class Key, class T> |
|
955 |
Q_OUTOFLINE_TEMPLATE std::map<Key, T> QMap<Key, T>::toStdMap() const |
|
956 |
{ |
|
957 |
std::map<Key, T> map; |
|
958 |
const_iterator it = end(); |
|
959 |
while (it != begin()) { |
|
960 |
--it; |
|
961 |
map.insert(std::pair<Key, T>(it.key(), it.value())); |
|
962 |
} |
|
963 |
return map; |
|
964 |
} |
|
965 |
||
966 |
#endif // QT_NO_STL |
|
967 |
||
968 |
template <class Key, class T> |
|
969 |
class QMultiMap : public QMap<Key, T> |
|
970 |
{ |
|
971 |
public: |
|
972 |
QMultiMap() {} |
|
973 |
QMultiMap(const QMap<Key, T> &other) : QMap<Key, T>(other) {} |
|
974 |
||
975 |
inline typename QMap<Key, T>::iterator replace(const Key &key, const T &value) |
|
976 |
{ return QMap<Key, T>::insert(key, value); } |
|
977 |
inline typename QMap<Key, T>::iterator insert(const Key &key, const T &value) |
|
978 |
{ return QMap<Key, T>::insertMulti(key, value); } |
|
979 |
||
980 |
inline QMultiMap &operator+=(const QMultiMap &other) |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
981 |
{ this->unite(other); return *this; } |
0 | 982 |
inline QMultiMap operator+(const QMultiMap &other) const |
983 |
{ QMultiMap result = *this; result += other; return result; } |
|
984 |
||
985 |
#if !defined(Q_NO_USING_KEYWORD) && !defined(Q_CC_RVCT) |
|
986 |
// RVCT compiler doesn't handle using-keyword right when used functions are overloaded in child class |
|
987 |
using QMap<Key, T>::contains; |
|
988 |
using QMap<Key, T>::remove; |
|
989 |
using QMap<Key, T>::count; |
|
990 |
using QMap<Key, T>::find; |
|
991 |
using QMap<Key, T>::constFind; |
|
992 |
#else |
|
993 |
inline bool contains(const Key &key) const |
|
994 |
{ return QMap<Key, T>::contains(key); } |
|
995 |
inline int remove(const Key &key) |
|
996 |
{ return QMap<Key, T>::remove(key); } |
|
997 |
inline int count(const Key &key) const |
|
998 |
{ return QMap<Key, T>::count(key); } |
|
999 |
inline int count() const |
|
1000 |
{ return QMap<Key, T>::count(); } |
|
1001 |
inline typename QMap<Key, T>::iterator find(const Key &key) |
|
1002 |
{ return QMap<Key, T>::find(key); } |
|
1003 |
inline typename QMap<Key, T>::const_iterator find(const Key &key) const |
|
1004 |
{ return QMap<Key, T>::find(key); } |
|
1005 |
inline typename QMap<Key, T>::const_iterator constFind(const Key &key) const |
|
1006 |
{ return QMap<Key, T>::constFind(key); } |
|
1007 |
#endif |
|
1008 |
||
1009 |
bool contains(const Key &key, const T &value) const; |
|
1010 |
||
1011 |
int remove(const Key &key, const T &value); |
|
1012 |
||
1013 |
int count(const Key &key, const T &value) const; |
|
1014 |
||
1015 |
typename QMap<Key, T>::iterator find(const Key &key, const T &value) { |
|
1016 |
typename QMap<Key, T>::iterator i(find(key)); |
|
1017 |
typename QMap<Key, T>::iterator end(this->end()); |
|
1018 |
while (i != end && !qMapLessThanKey<Key>(key, i.key())) { |
|
1019 |
if (i.value() == value) |
|
1020 |
return i; |
|
1021 |
++i; |
|
1022 |
} |
|
1023 |
return end; |
|
1024 |
} |
|
1025 |
typename QMap<Key, T>::const_iterator find(const Key &key, const T &value) const { |
|
1026 |
typename QMap<Key, T>::const_iterator i(constFind(key)); |
|
1027 |
typename QMap<Key, T>::const_iterator end(QMap<Key, T>::constEnd()); |
|
1028 |
while (i != end && !qMapLessThanKey<Key>(key, i.key())) { |
|
1029 |
if (i.value() == value) |
|
1030 |
return i; |
|
1031 |
++i; |
|
1032 |
} |
|
1033 |
return end; |
|
1034 |
} |
|
1035 |
typename QMap<Key, T>::const_iterator constFind(const Key &key, const T &value) const |
|
1036 |
{ return find(key, value); } |
|
1037 |
private: |
|
1038 |
T &operator[](const Key &key); |
|
1039 |
const T operator[](const Key &key) const; |
|
1040 |
}; |
|
1041 |
||
1042 |
template <class Key, class T> |
|
1043 |
Q_INLINE_TEMPLATE bool QMultiMap<Key, T>::contains(const Key &key, const T &value) const |
|
1044 |
{ |
|
1045 |
return constFind(key, value) != QMap<Key, T>::constEnd(); |
|
1046 |
} |
|
1047 |
||
1048 |
template <class Key, class T> |
|
1049 |
Q_INLINE_TEMPLATE int QMultiMap<Key, T>::remove(const Key &key, const T &value) |
|
1050 |
{ |
|
1051 |
int n = 0; |
|
1052 |
typename QMap<Key, T>::iterator i(find(key)); |
|
1053 |
typename QMap<Key, T>::iterator end(QMap<Key, T>::end()); |
|
1054 |
while (i != end && !qMapLessThanKey<Key>(key, i.key())) { |
|
1055 |
if (i.value() == value) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
1056 |
i = this->erase(i); |
0 | 1057 |
++n; |
1058 |
} else { |
|
1059 |
++i; |
|
1060 |
} |
|
1061 |
} |
|
1062 |
return n; |
|
1063 |
} |
|
1064 |
||
1065 |
template <class Key, class T> |
|
1066 |
Q_INLINE_TEMPLATE int QMultiMap<Key, T>::count(const Key &key, const T &value) const |
|
1067 |
{ |
|
1068 |
int n = 0; |
|
1069 |
typename QMap<Key, T>::const_iterator i(constFind(key)); |
|
1070 |
typename QMap<Key, T>::const_iterator end(QMap<Key, T>::constEnd()); |
|
1071 |
while (i != end && !qMapLessThanKey<Key>(key, i.key())) { |
|
1072 |
if (i.value() == value) |
|
1073 |
++n; |
|
1074 |
++i; |
|
1075 |
} |
|
1076 |
return n; |
|
1077 |
} |
|
1078 |
||
1079 |
Q_DECLARE_ASSOCIATIVE_ITERATOR(Map) |
|
1080 |
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Map) |
|
1081 |
||
1082 |
QT_END_NAMESPACE |
|
1083 |
||
1084 |
QT_END_HEADER |
|
1085 |
||
1086 |
#endif // QMAP_H |