author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
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 QHASH_H |
|
43 |
#define QHASH_H |
|
44 |
||
45 |
#include <QtCore/qatomic.h> |
|
46 |
#include <QtCore/qchar.h> |
|
47 |
#include <QtCore/qiterator.h> |
|
48 |
#include <QtCore/qlist.h> |
|
49 |
#include <QtCore/qpair.h> |
|
50 |
||
51 |
QT_BEGIN_HEADER |
|
52 |
||
53 |
QT_BEGIN_NAMESPACE |
|
54 |
||
55 |
QT_MODULE(Core) |
|
56 |
||
57 |
class QBitArray; |
|
58 |
class QByteArray; |
|
59 |
class QString; |
|
60 |
class QStringRef; |
|
61 |
||
62 |
inline uint qHash(char key) { return uint(key); } |
|
63 |
inline uint qHash(uchar key) { return uint(key); } |
|
64 |
inline uint qHash(signed char key) { return uint(key); } |
|
65 |
inline uint qHash(ushort key) { return uint(key); } |
|
66 |
inline uint qHash(short key) { return uint(key); } |
|
67 |
inline uint qHash(uint key) { return key; } |
|
68 |
inline uint qHash(int key) { return uint(key); } |
|
69 |
inline uint qHash(ulong key) |
|
70 |
{ |
|
71 |
if (sizeof(ulong) > sizeof(uint)) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
72 |
return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)); |
0 | 73 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
74 |
return uint(key & (~0U)); |
0 | 75 |
} |
76 |
} |
|
77 |
inline uint qHash(long key) { return qHash(ulong(key)); } |
|
78 |
inline uint qHash(quint64 key) |
|
79 |
{ |
|
80 |
if (sizeof(quint64) > sizeof(uint)) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
81 |
return uint(((key >> (8 * sizeof(uint) - 1)) ^ key) & (~0U)); |
0 | 82 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
83 |
return uint(key & (~0U)); |
0 | 84 |
} |
85 |
} |
|
86 |
inline uint qHash(qint64 key) { return qHash(quint64(key)); } |
|
87 |
inline uint qHash(QChar key) { return qHash(key.unicode()); } |
|
88 |
Q_CORE_EXPORT uint qHash(const QByteArray &key); |
|
89 |
Q_CORE_EXPORT uint qHash(const QString &key); |
|
90 |
Q_CORE_EXPORT uint qHash(const QStringRef &key); |
|
91 |
Q_CORE_EXPORT uint qHash(const QBitArray &key); |
|
92 |
||
93 |
#if defined(Q_CC_MSVC) |
|
94 |
#pragma warning( push ) |
|
95 |
#pragma warning( disable : 4311 ) // disable pointer truncation warning |
|
96 |
#endif |
|
97 |
template <class T> inline uint qHash(const T *key) |
|
98 |
{ |
|
99 |
return qHash(reinterpret_cast<quintptr>(key)); |
|
100 |
} |
|
101 |
#if defined(Q_CC_MSVC) |
|
102 |
#pragma warning( pop ) |
|
103 |
#endif |
|
104 |
||
105 |
template <typename T1, typename T2> inline uint qHash(const QPair<T1, T2> &key) |
|
106 |
{ |
|
107 |
uint h1 = qHash(key.first); |
|
108 |
uint h2 = qHash(key.second); |
|
109 |
return ((h1 << 16) | (h1 >> 16)) ^ h2; |
|
110 |
} |
|
111 |
||
112 |
struct Q_CORE_EXPORT QHashData |
|
113 |
{ |
|
114 |
struct Node { |
|
115 |
Node *next; |
|
116 |
uint h; |
|
117 |
}; |
|
118 |
||
119 |
Node *fakeNext; |
|
120 |
Node **buckets; |
|
121 |
QBasicAtomicInt ref; |
|
122 |
int size; |
|
123 |
int nodeSize; |
|
124 |
short userNumBits; |
|
125 |
short numBits; |
|
126 |
int numBuckets; |
|
127 |
uint sharable : 1; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
128 |
uint strictAlignment : 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
129 |
uint reserved : 30; |
0 | 130 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
131 |
void *allocateNode(); // ### Qt5 remove me |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
132 |
void *allocateNode(int nodeAlign); |
0 | 133 |
void freeNode(void *node); |
134 |
QHashData *detach_helper(void (*node_duplicate)(Node *, void *), int nodeSize); // ### Qt5 remove me |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
QHashData *detach_helper2(void (*node_duplicate)(Node *, void *), void (*node_delete)(Node *), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
int nodeSize, int nodeAlign); |
0 | 137 |
void mightGrow(); |
138 |
bool willGrow(); |
|
139 |
void hasShrunk(); |
|
140 |
void rehash(int hint); |
|
141 |
void free_helper(void (*node_delete)(Node *)); |
|
142 |
void destroyAndFree(); // ### Qt5 remove me |
|
143 |
Node *firstNode(); |
|
144 |
#ifdef QT_QHASH_DEBUG |
|
145 |
void dump(); |
|
146 |
void checkSanity(); |
|
147 |
#endif |
|
148 |
static Node *nextNode(Node *node); |
|
149 |
static Node *previousNode(Node *node); |
|
150 |
||
151 |
static QHashData shared_null; |
|
152 |
}; |
|
153 |
||
154 |
inline void QHashData::mightGrow() // ### Qt 5: eliminate |
|
155 |
{ |
|
156 |
if (size >= numBuckets) |
|
157 |
rehash(numBits + 1); |
|
158 |
} |
|
159 |
||
160 |
inline bool QHashData::willGrow() |
|
161 |
{ |
|
162 |
if (size >= numBuckets) { |
|
163 |
rehash(numBits + 1); |
|
164 |
return true; |
|
165 |
} else { |
|
166 |
return false; |
|
167 |
} |
|
168 |
} |
|
169 |
||
170 |
inline void QHashData::hasShrunk() |
|
171 |
{ |
|
172 |
if (size <= (numBuckets >> 3) && numBits > userNumBits) { |
|
173 |
QT_TRY { |
|
174 |
rehash(qMax(int(numBits) - 2, int(userNumBits))); |
|
175 |
} QT_CATCH(const std::bad_alloc &) { |
|
176 |
// ignore bad allocs - shrinking shouldn't throw. rehash is exception safe. |
|
177 |
} |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
inline QHashData::Node *QHashData::firstNode() |
|
182 |
{ |
|
183 |
Node *e = reinterpret_cast<Node *>(this); |
|
184 |
Node **bucket = buckets; |
|
185 |
int n = numBuckets; |
|
186 |
while (n--) { |
|
187 |
if (*bucket != e) |
|
188 |
return *bucket; |
|
189 |
++bucket; |
|
190 |
} |
|
191 |
return e; |
|
192 |
} |
|
193 |
||
194 |
struct QHashDummyValue |
|
195 |
{ |
|
196 |
}; |
|
197 |
||
198 |
inline bool operator==(const QHashDummyValue & /* v1 */, const QHashDummyValue & /* v2 */) |
|
199 |
{ |
|
200 |
return true; |
|
201 |
} |
|
202 |
||
203 |
Q_DECLARE_TYPEINFO(QHashDummyValue, Q_MOVABLE_TYPE | Q_DUMMY_TYPE); |
|
204 |
||
205 |
template <class Key, class T> |
|
206 |
struct QHashDummyNode |
|
207 |
{ |
|
208 |
QHashDummyNode *next; |
|
209 |
uint h; |
|
210 |
Key key; |
|
211 |
||
212 |
inline QHashDummyNode(const Key &key0) : key(key0) {} |
|
213 |
}; |
|
214 |
||
215 |
template <class Key, class T> |
|
216 |
struct QHashNode |
|
217 |
{ |
|
218 |
QHashNode *next; |
|
219 |
uint h; |
|
220 |
Key key; |
|
221 |
T value; |
|
222 |
||
223 |
inline QHashNode(const Key &key0) : key(key0) {} // ### remove in 5.0 |
|
224 |
inline QHashNode(const Key &key0, const T &value0) : key(key0), value(value0) {} |
|
225 |
inline bool same_key(uint h0, const Key &key0) { return h0 == h && key0 == key; } |
|
226 |
}; |
|
227 |
||
228 |
#ifndef QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION |
|
229 |
#define Q_HASH_DECLARE_INT_NODES(key_type) \ |
|
230 |
template <class T> \ |
|
231 |
struct QHashDummyNode<key_type, T> { \ |
|
232 |
QHashDummyNode *next; \ |
|
233 |
union { uint h; key_type key; }; \ |
|
234 |
\ |
|
235 |
inline QHashDummyNode(key_type /* key0 */) {} \ |
|
236 |
}; \ |
|
237 |
\ |
|
238 |
template <class T> \ |
|
239 |
struct QHashNode<key_type, T> { \ |
|
240 |
QHashNode *next; \ |
|
241 |
union { uint h; key_type key; }; \ |
|
242 |
T value; \ |
|
243 |
\ |
|
244 |
inline QHashNode(key_type /* key0 */) {} \ |
|
245 |
inline QHashNode(key_type /* key0 */, const T &value0) : value(value0) {} \ |
|
246 |
inline bool same_key(uint h0, key_type) { return h0 == h; } \ |
|
247 |
} |
|
248 |
||
249 |
#if defined(Q_BYTE_ORDER) && Q_BYTE_ORDER == Q_LITTLE_ENDIAN |
|
250 |
Q_HASH_DECLARE_INT_NODES(short); |
|
251 |
Q_HASH_DECLARE_INT_NODES(ushort); |
|
252 |
#endif |
|
253 |
Q_HASH_DECLARE_INT_NODES(int); |
|
254 |
Q_HASH_DECLARE_INT_NODES(uint); |
|
255 |
#undef Q_HASH_DECLARE_INT_NODES |
|
256 |
#endif // QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION |
|
257 |
||
258 |
template <class Key, class T> |
|
259 |
class QHash |
|
260 |
{ |
|
261 |
typedef QHashDummyNode<Key, T> DummyNode; |
|
262 |
typedef QHashNode<Key, T> Node; |
|
263 |
||
264 |
union { |
|
265 |
QHashData *d; |
|
266 |
QHashNode<Key, T> *e; |
|
267 |
}; |
|
268 |
||
269 |
static inline Node *concrete(QHashData::Node *node) { |
|
270 |
return reinterpret_cast<Node *>(node); |
|
271 |
} |
|
272 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
#ifdef Q_ALIGNOF |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
static inline int alignOfNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(Node)); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
static inline int alignOfDummyNode() { return qMax<int>(sizeof(void*), Q_ALIGNOF(DummyNode)); } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
static inline int alignOfNode() { return 0; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
static inline int alignOfDummyNode() { return 0; } |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
|
0 | 281 |
public: |
282 |
inline QHash() : d(&QHashData::shared_null) { d->ref.ref(); } |
|
283 |
inline QHash(const QHash<Key, T> &other) : d(other.d) { d->ref.ref(); if (!d->sharable) detach(); } |
|
284 |
inline ~QHash() { if (!d->ref.deref()) freeData(d); } |
|
285 |
||
286 |
QHash<Key, T> &operator=(const QHash<Key, T> &other); |
|
287 |
||
288 |
bool operator==(const QHash<Key, T> &other) const; |
|
289 |
inline bool operator!=(const QHash<Key, T> &other) const { return !(*this == other); } |
|
290 |
||
291 |
inline int size() const { return d->size; } |
|
292 |
||
293 |
inline bool isEmpty() const { return d->size == 0; } |
|
294 |
||
295 |
inline int capacity() const { return d->numBuckets; } |
|
296 |
void reserve(int size); |
|
297 |
inline void squeeze() { reserve(1); } |
|
298 |
||
299 |
inline void detach() { if (d->ref != 1) detach_helper(); } |
|
300 |
inline bool isDetached() const { return d->ref == 1; } |
|
301 |
inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; } |
|
302 |
||
303 |
void clear(); |
|
304 |
||
305 |
int remove(const Key &key); |
|
306 |
T take(const Key &key); |
|
307 |
||
308 |
bool contains(const Key &key) const; |
|
309 |
const Key key(const T &value) const; |
|
310 |
const Key key(const T &value, const Key &defaultKey) const; |
|
311 |
const T value(const Key &key) const; |
|
312 |
const T value(const Key &key, const T &defaultValue) const; |
|
313 |
T &operator[](const Key &key); |
|
314 |
const T operator[](const Key &key) const; |
|
315 |
||
316 |
QList<Key> uniqueKeys() const; |
|
317 |
QList<Key> keys() const; |
|
318 |
QList<Key> keys(const T &value) const; |
|
319 |
QList<T> values() const; |
|
320 |
QList<T> values(const Key &key) const; |
|
321 |
int count(const Key &key) const; |
|
322 |
||
323 |
class const_iterator; |
|
324 |
||
325 |
class iterator |
|
326 |
{ |
|
327 |
friend class const_iterator; |
|
328 |
QHashData::Node *i; |
|
329 |
||
330 |
public: |
|
331 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
332 |
typedef ptrdiff_t difference_type; |
|
333 |
typedef T value_type; |
|
334 |
typedef T *pointer; |
|
335 |
typedef T &reference; |
|
336 |
||
337 |
// ### Qt 5: get rid of 'operator Node *' |
|
338 |
inline operator Node *() const { return concrete(i); } |
|
339 |
inline iterator() : i(0) { } |
|
340 |
explicit inline iterator(void *node) : i(reinterpret_cast<QHashData::Node *>(node)) { } |
|
341 |
||
342 |
inline const Key &key() const { return concrete(i)->key; } |
|
343 |
inline T &value() const { return concrete(i)->value; } |
|
344 |
inline T &operator*() const { return concrete(i)->value; } |
|
345 |
inline T *operator->() const { return &concrete(i)->value; } |
|
346 |
inline bool operator==(const iterator &o) const { return i == o.i; } |
|
347 |
inline bool operator!=(const iterator &o) const { return i != o.i; } |
|
348 |
||
349 |
inline iterator &operator++() { |
|
350 |
i = QHashData::nextNode(i); |
|
351 |
return *this; |
|
352 |
} |
|
353 |
inline iterator operator++(int) { |
|
354 |
iterator r = *this; |
|
355 |
i = QHashData::nextNode(i); |
|
356 |
return r; |
|
357 |
} |
|
358 |
inline iterator &operator--() { |
|
359 |
i = QHashData::previousNode(i); |
|
360 |
return *this; |
|
361 |
} |
|
362 |
inline iterator operator--(int) { |
|
363 |
iterator r = *this; |
|
364 |
i = QHashData::previousNode(i); |
|
365 |
return r; |
|
366 |
} |
|
367 |
inline iterator operator+(int j) const |
|
368 |
{ iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } |
|
369 |
inline iterator operator-(int j) const { return operator+(-j); } |
|
370 |
inline iterator &operator+=(int j) { return *this = *this + j; } |
|
371 |
inline iterator &operator-=(int j) { return *this = *this - j; } |
|
372 |
||
373 |
// ### Qt 5: not sure this is necessary anymore |
|
374 |
#ifdef QT_STRICT_ITERATORS |
|
375 |
private: |
|
376 |
#else |
|
377 |
public: |
|
378 |
#endif |
|
379 |
inline bool operator==(const const_iterator &o) const |
|
380 |
{ return i == o.i; } |
|
381 |
inline bool operator!=(const const_iterator &o) const |
|
382 |
{ return i != o.i; } |
|
383 |
||
384 |
private: |
|
385 |
// ### Qt 5: remove |
|
386 |
inline operator bool() const { return false; } |
|
387 |
}; |
|
388 |
friend class iterator; |
|
389 |
||
390 |
class const_iterator |
|
391 |
{ |
|
392 |
friend class iterator; |
|
393 |
QHashData::Node *i; |
|
394 |
||
395 |
public: |
|
396 |
typedef std::bidirectional_iterator_tag iterator_category; |
|
397 |
typedef ptrdiff_t difference_type; |
|
398 |
typedef T value_type; |
|
399 |
typedef const T *pointer; |
|
400 |
typedef const T &reference; |
|
401 |
||
402 |
// ### Qt 5: get rid of 'operator Node *' |
|
403 |
inline operator Node *() const { return concrete(i); } |
|
404 |
inline const_iterator() : i(0) { } |
|
405 |
explicit inline const_iterator(void *node) |
|
406 |
: i(reinterpret_cast<QHashData::Node *>(node)) { } |
|
407 |
#ifdef QT_STRICT_ITERATORS |
|
408 |
explicit inline const_iterator(const iterator &o) |
|
409 |
#else |
|
410 |
inline const_iterator(const iterator &o) |
|
411 |
#endif |
|
412 |
{ i = o.i; } |
|
413 |
||
414 |
inline const Key &key() const { return concrete(i)->key; } |
|
415 |
inline const T &value() const { return concrete(i)->value; } |
|
416 |
inline const T &operator*() const { return concrete(i)->value; } |
|
417 |
inline const T *operator->() const { return &concrete(i)->value; } |
|
418 |
inline bool operator==(const const_iterator &o) const { return i == o.i; } |
|
419 |
inline bool operator!=(const const_iterator &o) const { return i != o.i; } |
|
420 |
||
421 |
inline const_iterator &operator++() { |
|
422 |
i = QHashData::nextNode(i); |
|
423 |
return *this; |
|
424 |
} |
|
425 |
inline const_iterator operator++(int) { |
|
426 |
const_iterator r = *this; |
|
427 |
i = QHashData::nextNode(i); |
|
428 |
return r; |
|
429 |
} |
|
430 |
inline const_iterator &operator--() { |
|
431 |
i = QHashData::previousNode(i); |
|
432 |
return *this; |
|
433 |
} |
|
434 |
inline const_iterator operator--(int) { |
|
435 |
const_iterator r = *this; |
|
436 |
i = QHashData::previousNode(i); |
|
437 |
return r; |
|
438 |
} |
|
439 |
inline const_iterator operator+(int j) const |
|
440 |
{ const_iterator r = *this; if (j > 0) while (j--) ++r; else while (j++) --r; return r; } |
|
441 |
inline const_iterator operator-(int j) const { return operator+(-j); } |
|
442 |
inline const_iterator &operator+=(int j) { return *this = *this + j; } |
|
443 |
inline const_iterator &operator-=(int j) { return *this = *this - j; } |
|
444 |
||
445 |
// ### Qt 5: not sure this is necessary anymore |
|
446 |
#ifdef QT_STRICT_ITERATORS |
|
447 |
private: |
|
448 |
inline bool operator==(const iterator &o) const { return operator==(const_iterator(o)); } |
|
449 |
inline bool operator!=(const iterator &o) const { return operator!=(const_iterator(o)); } |
|
450 |
#endif |
|
451 |
||
452 |
private: |
|
453 |
// ### Qt 5: remove |
|
454 |
inline operator bool() const { return false; } |
|
455 |
}; |
|
456 |
friend class const_iterator; |
|
457 |
||
458 |
// STL style |
|
459 |
inline iterator begin() { detach(); return iterator(d->firstNode()); } |
|
460 |
inline const_iterator begin() const { return const_iterator(d->firstNode()); } |
|
461 |
inline const_iterator constBegin() const { return const_iterator(d->firstNode()); } |
|
462 |
inline iterator end() { detach(); return iterator(e); } |
|
463 |
inline const_iterator end() const { return const_iterator(e); } |
|
464 |
inline const_iterator constEnd() const { return const_iterator(e); } |
|
465 |
iterator erase(iterator it); |
|
466 |
||
467 |
// more Qt |
|
468 |
typedef iterator Iterator; |
|
469 |
typedef const_iterator ConstIterator; |
|
470 |
inline int count() const { return d->size; } |
|
471 |
iterator find(const Key &key); |
|
472 |
const_iterator find(const Key &key) const; |
|
473 |
const_iterator constFind(const Key &key) const; |
|
474 |
iterator insert(const Key &key, const T &value); |
|
475 |
iterator insertMulti(const Key &key, const T &value); |
|
476 |
QHash<Key, T> &unite(const QHash<Key, T> &other); |
|
477 |
||
478 |
// STL compatibility |
|
479 |
typedef T mapped_type; |
|
480 |
typedef Key key_type; |
|
481 |
typedef ptrdiff_t difference_type; |
|
482 |
typedef int size_type; |
|
483 |
||
484 |
inline bool empty() const { return isEmpty(); } |
|
485 |
||
486 |
#ifdef QT_QHASH_DEBUG |
|
487 |
inline void dump() const { d->dump(); } |
|
488 |
inline void checkSanity() const { d->checkSanity(); } |
|
489 |
#endif |
|
490 |
||
491 |
private: |
|
492 |
void detach_helper(); |
|
493 |
void freeData(QHashData *d); |
|
494 |
Node **findNode(const Key &key, uint *hp = 0) const; |
|
495 |
Node *createNode(uint h, const Key &key, const T &value, Node **nextNode); |
|
496 |
void deleteNode(Node *node); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
497 |
static void deleteNode2(QHashData::Node *node); |
0 | 498 |
|
499 |
static void duplicateNode(QHashData::Node *originalNode, void *newNode); |
|
500 |
}; |
|
501 |
||
502 |
||
503 |
template <class Key, class T> |
|
504 |
Q_INLINE_TEMPLATE void QHash<Key, T>::deleteNode(Node *node) |
|
505 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
506 |
deleteNode2(reinterpret_cast<QHashData::Node*>(node)); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
507 |
d->freeNode(node); |
0 | 508 |
} |
509 |
||
510 |
template <class Key, class T> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
511 |
Q_INLINE_TEMPLATE void QHash<Key, T>::deleteNode2(QHashData::Node *node) |
0 | 512 |
{ |
513 |
#ifdef Q_CC_BOR |
|
514 |
concrete(node)->~QHashNode<Key, T>(); |
|
515 |
#elif defined(QT_NO_PARTIAL_TEMPLATE_SPECIALIZATION) |
|
516 |
concrete(node)->~QHashNode(); |
|
517 |
#else |
|
518 |
concrete(node)->~Node(); |
|
519 |
#endif |
|
520 |
} |
|
521 |
||
522 |
template <class Key, class T> |
|
523 |
Q_INLINE_TEMPLATE void QHash<Key, T>::duplicateNode(QHashData::Node *node, void *newNode) |
|
524 |
{ |
|
525 |
Node *concreteNode = concrete(node); |
|
526 |
if (QTypeInfo<T>::isDummy) { |
|
527 |
(void) new (newNode) DummyNode(concreteNode->key); |
|
528 |
} else { |
|
529 |
(void) new (newNode) Node(concreteNode->key, concreteNode->value); |
|
530 |
} |
|
531 |
} |
|
532 |
||
533 |
template <class Key, class T> |
|
534 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::Node * |
|
535 |
QHash<Key, T>::createNode(uint ah, const Key &akey, const T &avalue, Node **anextNode) |
|
536 |
{ |
|
537 |
Node *node; |
|
538 |
||
539 |
if (QTypeInfo<T>::isDummy) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
540 |
node = reinterpret_cast<Node *>(new (d->allocateNode(alignOfDummyNode())) DummyNode(akey)); |
0 | 541 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
542 |
node = new (d->allocateNode(alignOfNode())) Node(akey, avalue); |
0 | 543 |
} |
544 |
||
545 |
node->h = ah; |
|
546 |
node->next = *anextNode; |
|
547 |
*anextNode = node; |
|
548 |
++d->size; |
|
549 |
return node; |
|
550 |
} |
|
551 |
||
552 |
template <class Key, class T> |
|
553 |
Q_INLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::unite(const QHash<Key, T> &other) |
|
554 |
{ |
|
555 |
QHash<Key, T> copy(other); |
|
556 |
const_iterator it = copy.constEnd(); |
|
557 |
while (it != copy.constBegin()) { |
|
558 |
--it; |
|
559 |
insertMulti(it.key(), it.value()); |
|
560 |
} |
|
561 |
return *this; |
|
562 |
} |
|
563 |
||
564 |
template <class Key, class T> |
|
565 |
Q_OUTOFLINE_TEMPLATE void QHash<Key, T>::freeData(QHashData *x) |
|
566 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
567 |
x->free_helper(deleteNode2); |
0 | 568 |
} |
569 |
||
570 |
template <class Key, class T> |
|
571 |
Q_INLINE_TEMPLATE void QHash<Key, T>::clear() |
|
572 |
{ |
|
573 |
*this = QHash<Key,T>(); |
|
574 |
} |
|
575 |
||
576 |
template <class Key, class T> |
|
577 |
Q_OUTOFLINE_TEMPLATE void QHash<Key, T>::detach_helper() |
|
578 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
579 |
QHashData *x = d->detach_helper2(duplicateNode, deleteNode2, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
580 |
QTypeInfo<T>::isDummy ? sizeof(DummyNode) : sizeof(Node), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
581 |
QTypeInfo<T>::isDummy ? alignOfDummyNode() : alignOfNode()); |
0 | 582 |
if (!d->ref.deref()) |
583 |
freeData(d); |
|
584 |
d = x; |
|
585 |
} |
|
586 |
||
587 |
template <class Key, class T> |
|
588 |
Q_INLINE_TEMPLATE QHash<Key, T> &QHash<Key, T>::operator=(const QHash<Key, T> &other) |
|
589 |
{ |
|
590 |
if (d != other.d) { |
|
591 |
other.d->ref.ref(); |
|
592 |
if (!d->ref.deref()) |
|
593 |
freeData(d); |
|
594 |
d = other.d; |
|
595 |
if (!d->sharable) |
|
596 |
detach_helper(); |
|
597 |
} |
|
598 |
return *this; |
|
599 |
} |
|
600 |
||
601 |
template <class Key, class T> |
|
602 |
Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey) const |
|
603 |
{ |
|
604 |
Node *node; |
|
605 |
if (d->size == 0 || (node = *findNode(akey)) == e) { |
|
606 |
return T(); |
|
607 |
} else { |
|
608 |
return node->value; |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
template <class Key, class T> |
|
613 |
Q_INLINE_TEMPLATE const T QHash<Key, T>::value(const Key &akey, const T &adefaultValue) const |
|
614 |
{ |
|
615 |
Node *node; |
|
616 |
if (d->size == 0 || (node = *findNode(akey)) == e) { |
|
617 |
return adefaultValue; |
|
618 |
} else { |
|
619 |
return node->value; |
|
620 |
} |
|
621 |
} |
|
622 |
||
623 |
template <class Key, class T> |
|
624 |
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::uniqueKeys() const |
|
625 |
{ |
|
626 |
QList<Key> res; |
|
627 |
const_iterator i = begin(); |
|
628 |
if (i != end()) { |
|
629 |
for (;;) { |
|
630 |
const Key &aKey = i.key(); |
|
631 |
res.append(aKey); |
|
632 |
do { |
|
633 |
if (++i == end()) |
|
634 |
goto break_out_of_outer_loop; |
|
635 |
} while (aKey == i.key()); |
|
636 |
} |
|
637 |
} |
|
638 |
break_out_of_outer_loop: |
|
639 |
return res; |
|
640 |
} |
|
641 |
||
642 |
template <class Key, class T> |
|
643 |
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys() const |
|
644 |
{ |
|
645 |
QList<Key> res; |
|
646 |
const_iterator i = begin(); |
|
647 |
while (i != end()) { |
|
648 |
res.append(i.key()); |
|
649 |
++i; |
|
650 |
} |
|
651 |
return res; |
|
652 |
} |
|
653 |
||
654 |
template <class Key, class T> |
|
655 |
Q_OUTOFLINE_TEMPLATE QList<Key> QHash<Key, T>::keys(const T &avalue) const |
|
656 |
{ |
|
657 |
QList<Key> res; |
|
658 |
const_iterator i = begin(); |
|
659 |
while (i != end()) { |
|
660 |
if (i.value() == avalue) |
|
661 |
res.append(i.key()); |
|
662 |
++i; |
|
663 |
} |
|
664 |
return res; |
|
665 |
} |
|
666 |
||
667 |
template <class Key, class T> |
|
668 |
Q_OUTOFLINE_TEMPLATE const Key QHash<Key, T>::key(const T &avalue) const |
|
669 |
{ |
|
670 |
return key(avalue, Key()); |
|
671 |
} |
|
672 |
||
673 |
template <class Key, class T> |
|
674 |
Q_OUTOFLINE_TEMPLATE const Key QHash<Key, T>::key(const T &avalue, const Key &defaultValue) const |
|
675 |
{ |
|
676 |
const_iterator i = begin(); |
|
677 |
while (i != end()) { |
|
678 |
if (i.value() == avalue) |
|
679 |
return i.key(); |
|
680 |
++i; |
|
681 |
} |
|
682 |
||
683 |
return defaultValue; |
|
684 |
} |
|
685 |
||
686 |
template <class Key, class T> |
|
687 |
Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values() const |
|
688 |
{ |
|
689 |
QList<T> res; |
|
690 |
const_iterator i = begin(); |
|
691 |
while (i != end()) { |
|
692 |
res.append(i.value()); |
|
693 |
++i; |
|
694 |
} |
|
695 |
return res; |
|
696 |
} |
|
697 |
||
698 |
template <class Key, class T> |
|
699 |
Q_OUTOFLINE_TEMPLATE QList<T> QHash<Key, T>::values(const Key &akey) const |
|
700 |
{ |
|
701 |
QList<T> res; |
|
702 |
Node *node = *findNode(akey); |
|
703 |
if (node != e) { |
|
704 |
do { |
|
705 |
res.append(node->value); |
|
706 |
} while ((node = node->next) != e && node->key == akey); |
|
707 |
} |
|
708 |
return res; |
|
709 |
} |
|
710 |
||
711 |
template <class Key, class T> |
|
712 |
Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::count(const Key &akey) const |
|
713 |
{ |
|
714 |
int cnt = 0; |
|
715 |
Node *node = *findNode(akey); |
|
716 |
if (node != e) { |
|
717 |
do { |
|
718 |
++cnt; |
|
719 |
} while ((node = node->next) != e && node->key == akey); |
|
720 |
} |
|
721 |
return cnt; |
|
722 |
} |
|
723 |
||
724 |
template <class Key, class T> |
|
725 |
Q_INLINE_TEMPLATE const T QHash<Key, T>::operator[](const Key &akey) const |
|
726 |
{ |
|
727 |
return value(akey); |
|
728 |
} |
|
729 |
||
730 |
template <class Key, class T> |
|
731 |
Q_INLINE_TEMPLATE T &QHash<Key, T>::operator[](const Key &akey) |
|
732 |
{ |
|
733 |
detach(); |
|
734 |
||
735 |
uint h; |
|
736 |
Node **node = findNode(akey, &h); |
|
737 |
if (*node == e) { |
|
738 |
if (d->willGrow()) |
|
739 |
node = findNode(akey, &h); |
|
740 |
return createNode(h, akey, T(), node)->value; |
|
741 |
} |
|
742 |
return (*node)->value; |
|
743 |
} |
|
744 |
||
745 |
template <class Key, class T> |
|
746 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insert(const Key &akey, |
|
747 |
const T &avalue) |
|
748 |
{ |
|
749 |
detach(); |
|
750 |
||
751 |
uint h; |
|
752 |
Node **node = findNode(akey, &h); |
|
753 |
if (*node == e) { |
|
754 |
if (d->willGrow()) |
|
755 |
node = findNode(akey, &h); |
|
756 |
return iterator(createNode(h, akey, avalue, node)); |
|
757 |
} |
|
758 |
||
759 |
if (!QTypeInfo<T>::isDummy) |
|
760 |
(*node)->value = avalue; |
|
761 |
return iterator(*node); |
|
762 |
} |
|
763 |
||
764 |
template <class Key, class T> |
|
765 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::insertMulti(const Key &akey, |
|
766 |
const T &avalue) |
|
767 |
{ |
|
768 |
detach(); |
|
769 |
d->willGrow(); |
|
770 |
||
771 |
uint h; |
|
772 |
Node **nextNode = findNode(akey, &h); |
|
773 |
return iterator(createNode(h, akey, avalue, nextNode)); |
|
774 |
} |
|
775 |
||
776 |
template <class Key, class T> |
|
777 |
Q_OUTOFLINE_TEMPLATE int QHash<Key, T>::remove(const Key &akey) |
|
778 |
{ |
|
779 |
if (isEmpty()) // prevents detaching shared null |
|
780 |
return 0; |
|
781 |
detach(); |
|
782 |
||
783 |
int oldSize = d->size; |
|
784 |
Node **node = findNode(akey); |
|
785 |
if (*node != e) { |
|
786 |
bool deleteNext = true; |
|
787 |
do { |
|
788 |
Node *next = (*node)->next; |
|
789 |
deleteNext = (next != e && next->key == (*node)->key); |
|
790 |
deleteNode(*node); |
|
791 |
*node = next; |
|
792 |
--d->size; |
|
793 |
} while (deleteNext); |
|
794 |
d->hasShrunk(); |
|
795 |
} |
|
796 |
return oldSize - d->size; |
|
797 |
} |
|
798 |
||
799 |
template <class Key, class T> |
|
800 |
Q_OUTOFLINE_TEMPLATE T QHash<Key, T>::take(const Key &akey) |
|
801 |
{ |
|
802 |
if (isEmpty()) // prevents detaching shared null |
|
803 |
return T(); |
|
804 |
detach(); |
|
805 |
||
806 |
Node **node = findNode(akey); |
|
807 |
if (*node != e) { |
|
808 |
T t = (*node)->value; |
|
809 |
Node *next = (*node)->next; |
|
810 |
deleteNode(*node); |
|
811 |
*node = next; |
|
812 |
--d->size; |
|
813 |
d->hasShrunk(); |
|
814 |
return t; |
|
815 |
} |
|
816 |
return T(); |
|
817 |
} |
|
818 |
||
819 |
template <class Key, class T> |
|
820 |
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::erase(iterator it) |
|
821 |
{ |
|
822 |
if (it == iterator(e)) |
|
823 |
return it; |
|
824 |
||
825 |
iterator ret = it; |
|
826 |
++ret; |
|
827 |
||
828 |
Node *node = it; |
|
829 |
Node **node_ptr = reinterpret_cast<Node **>(&d->buckets[node->h % d->numBuckets]); |
|
830 |
while (*node_ptr != node) |
|
831 |
node_ptr = &(*node_ptr)->next; |
|
832 |
*node_ptr = node->next; |
|
833 |
deleteNode(node); |
|
834 |
--d->size; |
|
835 |
return ret; |
|
836 |
} |
|
837 |
||
838 |
template <class Key, class T> |
|
839 |
Q_INLINE_TEMPLATE void QHash<Key, T>::reserve(int asize) |
|
840 |
{ |
|
841 |
detach(); |
|
842 |
d->rehash(-qMax(asize, 1)); |
|
843 |
} |
|
844 |
||
845 |
template <class Key, class T> |
|
846 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::const_iterator QHash<Key, T>::find(const Key &akey) const |
|
847 |
{ |
|
848 |
return const_iterator(*findNode(akey)); |
|
849 |
} |
|
850 |
||
851 |
template <class Key, class T> |
|
852 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::const_iterator QHash<Key, T>::constFind(const Key &akey) const |
|
853 |
{ |
|
854 |
return const_iterator(*findNode(akey)); |
|
855 |
} |
|
856 |
||
857 |
template <class Key, class T> |
|
858 |
Q_INLINE_TEMPLATE typename QHash<Key, T>::iterator QHash<Key, T>::find(const Key &akey) |
|
859 |
{ |
|
860 |
detach(); |
|
861 |
return iterator(*findNode(akey)); |
|
862 |
} |
|
863 |
||
864 |
template <class Key, class T> |
|
865 |
Q_INLINE_TEMPLATE bool QHash<Key, T>::contains(const Key &akey) const |
|
866 |
{ |
|
867 |
return *findNode(akey) != e; |
|
868 |
} |
|
869 |
||
870 |
template <class Key, class T> |
|
871 |
Q_OUTOFLINE_TEMPLATE typename QHash<Key, T>::Node **QHash<Key, T>::findNode(const Key &akey, |
|
872 |
uint *ahp) const |
|
873 |
{ |
|
874 |
Node **node; |
|
875 |
uint h = qHash(akey); |
|
876 |
||
877 |
if (d->numBuckets) { |
|
878 |
node = reinterpret_cast<Node **>(&d->buckets[h % d->numBuckets]); |
|
879 |
Q_ASSERT(*node == e || (*node)->next); |
|
880 |
while (*node != e && !(*node)->same_key(h, akey)) |
|
881 |
node = &(*node)->next; |
|
882 |
} else { |
|
883 |
node = const_cast<Node **>(reinterpret_cast<const Node * const *>(&e)); |
|
884 |
} |
|
885 |
if (ahp) |
|
886 |
*ahp = h; |
|
887 |
return node; |
|
888 |
} |
|
889 |
||
890 |
template <class Key, class T> |
|
891 |
Q_OUTOFLINE_TEMPLATE bool QHash<Key, T>::operator==(const QHash<Key, T> &other) const |
|
892 |
{ |
|
893 |
if (size() != other.size()) |
|
894 |
return false; |
|
895 |
if (d == other.d) |
|
896 |
return true; |
|
897 |
||
898 |
const_iterator it = begin(); |
|
899 |
||
900 |
while (it != end()) { |
|
901 |
const Key &akey = it.key(); |
|
902 |
||
903 |
const_iterator it2 = other.find(akey); |
|
904 |
do { |
|
905 |
if (it2 == other.end() || !(it2.key() == akey)) |
|
906 |
return false; |
|
907 |
if (!QTypeInfo<T>::isDummy && !(it.value() == it2.value())) |
|
908 |
return false; |
|
909 |
++it; |
|
910 |
++it2; |
|
911 |
} while (it != end() && it.key() == akey); |
|
912 |
} |
|
913 |
return true; |
|
914 |
} |
|
915 |
||
916 |
template <class Key, class T> |
|
917 |
class QMultiHash : public QHash<Key, T> |
|
918 |
{ |
|
919 |
public: |
|
920 |
QMultiHash() {} |
|
921 |
QMultiHash(const QHash<Key, T> &other) : QHash<Key, T>(other) {} |
|
922 |
||
923 |
inline typename QHash<Key, T>::iterator replace(const Key &key, const T &value) |
|
924 |
{ return QHash<Key, T>::insert(key, value); } |
|
925 |
||
926 |
inline typename QHash<Key, T>::iterator insert(const Key &key, const T &value) |
|
927 |
{ return QHash<Key, T>::insertMulti(key, value); } |
|
928 |
||
929 |
inline QMultiHash &operator+=(const QMultiHash &other) |
|
930 |
{ unite(other); return *this; } |
|
931 |
inline QMultiHash operator+(const QMultiHash &other) const |
|
932 |
{ QMultiHash result = *this; result += other; return result; } |
|
933 |
||
934 |
#if !defined(Q_NO_USING_KEYWORD) && !defined(Q_CC_RVCT) |
|
935 |
// RVCT compiler doesn't handle using-keyword right when used functions are overloaded in child class |
|
936 |
using QHash<Key, T>::contains; |
|
937 |
using QHash<Key, T>::remove; |
|
938 |
using QHash<Key, T>::count; |
|
939 |
using QHash<Key, T>::find; |
|
940 |
using QHash<Key, T>::constFind; |
|
941 |
#else |
|
942 |
inline bool contains(const Key &key) const |
|
943 |
{ return QHash<Key, T>::contains(key); } |
|
944 |
inline int remove(const Key &key) |
|
945 |
{ return QHash<Key, T>::remove(key); } |
|
946 |
inline int count(const Key &key) const |
|
947 |
{ return QHash<Key, T>::count(key); } |
|
948 |
inline int count() const |
|
949 |
{ return QHash<Key, T>::count(); } |
|
950 |
inline typename QHash<Key, T>::iterator find(const Key &key) |
|
951 |
{ return QHash<Key, T>::find(key); } |
|
952 |
inline typename QHash<Key, T>::const_iterator find(const Key &key) const |
|
953 |
{ return QHash<Key, T>::find(key); } |
|
954 |
inline typename QHash<Key, T>::const_iterator constFind(const Key &key) const |
|
955 |
{ return QHash<Key, T>::constFind(key); } |
|
956 |
#endif |
|
957 |
||
958 |
bool contains(const Key &key, const T &value) const; |
|
959 |
||
960 |
int remove(const Key &key, const T &value); |
|
961 |
||
962 |
int count(const Key &key, const T &value) const; |
|
963 |
||
964 |
typename QHash<Key, T>::iterator find(const Key &key, const T &value) { |
|
965 |
typename QHash<Key, T>::iterator i(find(key)); |
|
966 |
typename QHash<Key, T>::iterator end(this->end()); |
|
967 |
while (i != end && i.key() == key) { |
|
968 |
if (i.value() == value) |
|
969 |
return i; |
|
970 |
++i; |
|
971 |
} |
|
972 |
return end; |
|
973 |
} |
|
974 |
typename QHash<Key, T>::const_iterator find(const Key &key, const T &value) const { |
|
975 |
typename QHash<Key, T>::const_iterator i(constFind(key)); |
|
976 |
typename QHash<Key, T>::const_iterator end(QHash<Key, T>::constEnd()); |
|
977 |
while (i != end && i.key() == key) { |
|
978 |
if (i.value() == value) |
|
979 |
return i; |
|
980 |
++i; |
|
981 |
} |
|
982 |
return end; |
|
983 |
} |
|
984 |
typename QHash<Key, T>::const_iterator constFind(const Key &key, const T &value) const |
|
985 |
{ return find(key, value); } |
|
986 |
private: |
|
987 |
T &operator[](const Key &key); |
|
988 |
const T operator[](const Key &key) const; |
|
989 |
}; |
|
990 |
||
991 |
template <class Key, class T> |
|
992 |
Q_INLINE_TEMPLATE bool QMultiHash<Key, T>::contains(const Key &key, const T &value) const |
|
993 |
{ |
|
994 |
return constFind(key, value) != QHash<Key, T>::constEnd(); |
|
995 |
} |
|
996 |
||
997 |
template <class Key, class T> |
|
998 |
Q_INLINE_TEMPLATE int QMultiHash<Key, T>::remove(const Key &key, const T &value) |
|
999 |
{ |
|
1000 |
int n = 0; |
|
1001 |
typename QHash<Key, T>::iterator i(find(key)); |
|
1002 |
typename QHash<Key, T>::iterator end(QHash<Key, T>::end()); |
|
1003 |
while (i != end && i.key() == key) { |
|
1004 |
if (i.value() == value) { |
|
1005 |
#if defined(Q_CC_RVCT) |
|
1006 |
// RVCT has problems with scoping, apparently. |
|
1007 |
i = QHash<Key, T>::erase(i); |
|
1008 |
#else |
|
1009 |
i = erase(i); |
|
1010 |
#endif |
|
1011 |
++n; |
|
1012 |
} else { |
|
1013 |
++i; |
|
1014 |
} |
|
1015 |
} |
|
1016 |
return n; |
|
1017 |
} |
|
1018 |
||
1019 |
template <class Key, class T> |
|
1020 |
Q_INLINE_TEMPLATE int QMultiHash<Key, T>::count(const Key &key, const T &value) const |
|
1021 |
{ |
|
1022 |
int n = 0; |
|
1023 |
typename QHash<Key, T>::const_iterator i(constFind(key)); |
|
1024 |
typename QHash<Key, T>::const_iterator end(QHash<Key, T>::constEnd()); |
|
1025 |
while (i != end && i.key() == key) { |
|
1026 |
if (i.value() == value) |
|
1027 |
++n; |
|
1028 |
++i; |
|
1029 |
} |
|
1030 |
return n; |
|
1031 |
} |
|
1032 |
||
1033 |
Q_DECLARE_ASSOCIATIVE_ITERATOR(Hash) |
|
1034 |
Q_DECLARE_MUTABLE_ASSOCIATIVE_ITERATOR(Hash) |
|
1035 |
||
1036 |
QT_END_NAMESPACE |
|
1037 |
||
1038 |
QT_END_HEADER |
|
1039 |
||
1040 |
#endif // QHASH_H |