author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 30 | 5dc02b23752f |
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 |
#include "qhash.h" |
|
43 |
||
44 |
#ifdef truncate |
|
45 |
#undef truncate |
|
46 |
#endif |
|
47 |
||
48 |
#include <qbitarray.h> |
|
49 |
#include <qstring.h> |
|
50 |
#include <stdlib.h> |
|
51 |
#ifdef QT_QHASH_DEBUG |
|
52 |
#include <qstring.h> |
|
53 |
#endif |
|
54 |
||
55 |
QT_BEGIN_NAMESPACE |
|
56 |
||
57 |
/* |
|
58 |
These functions are based on Peter J. Weinberger's hash function |
|
59 |
(from the Dragon Book). The constant 24 in the original function |
|
60 |
was replaced with 23 to produce fewer collisions on input such as |
|
61 |
"a", "aa", "aaa", "aaaa", ... |
|
62 |
*/ |
|
63 |
||
64 |
static uint hash(const uchar *p, int n) |
|
65 |
{ |
|
66 |
uint h = 0; |
|
67 |
||
68 |
while (n--) { |
|
69 |
h = (h << 4) + *p++; |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
70 |
h ^= (h & 0xf0000000) >> 23; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
71 |
h &= 0x0fffffff; |
0 | 72 |
} |
73 |
return h; |
|
74 |
} |
|
75 |
||
76 |
static uint hash(const QChar *p, int n) |
|
77 |
{ |
|
78 |
uint h = 0; |
|
79 |
||
80 |
while (n--) { |
|
81 |
h = (h << 4) + (*p++).unicode(); |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
82 |
h ^= (h & 0xf0000000) >> 23; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
83 |
h &= 0x0fffffff; |
0 | 84 |
} |
85 |
return h; |
|
86 |
} |
|
87 |
||
88 |
uint qHash(const QByteArray &key) |
|
89 |
{ |
|
90 |
return hash(reinterpret_cast<const uchar *>(key.constData()), key.size()); |
|
91 |
} |
|
92 |
||
93 |
uint qHash(const QString &key) |
|
94 |
{ |
|
95 |
return hash(key.unicode(), key.size()); |
|
96 |
} |
|
97 |
||
98 |
uint qHash(const QStringRef &key) |
|
99 |
{ |
|
100 |
return hash(key.unicode(), key.size()); |
|
101 |
} |
|
102 |
||
103 |
uint qHash(const QBitArray &bitArray) |
|
104 |
{ |
|
105 |
int m = bitArray.d.size() - 1; |
|
106 |
uint result = hash(reinterpret_cast<const uchar *>(bitArray.d.constData()), qMax(0, m)); |
|
107 |
||
108 |
// deal with the last 0 to 7 bits manually, because we can't trust that |
|
109 |
// the padding is initialized to 0 in bitArray.d |
|
110 |
int n = bitArray.size(); |
|
111 |
if (n & 0x7) |
|
112 |
result = ((result << 4) + bitArray.d.at(m)) & ((1 << n) - 1); |
|
113 |
return result; |
|
114 |
} |
|
115 |
||
116 |
/* |
|
117 |
The prime_deltas array is a table of selected prime values, even |
|
118 |
though it doesn't look like one. The primes we are using are 1, |
|
119 |
2, 5, 11, 17, 37, 67, 131, 257, ..., i.e. primes in the immediate |
|
120 |
surrounding of a power of two. |
|
121 |
||
122 |
The primeForNumBits() function returns the prime associated to a |
|
123 |
power of two. For example, primeForNumBits(8) returns 257. |
|
124 |
*/ |
|
125 |
||
126 |
static const uchar prime_deltas[] = { |
|
127 |
0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3, |
|
128 |
1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0 |
|
129 |
}; |
|
130 |
||
131 |
static inline int primeForNumBits(int numBits) |
|
132 |
{ |
|
133 |
return (1 << numBits) + prime_deltas[numBits]; |
|
134 |
} |
|
135 |
||
136 |
/* |
|
137 |
Returns the smallest integer n such that |
|
138 |
primeForNumBits(n) >= hint. |
|
139 |
*/ |
|
140 |
static int countBits(int hint) |
|
141 |
{ |
|
142 |
int numBits = 0; |
|
143 |
int bits = hint; |
|
144 |
||
145 |
while (bits > 1) { |
|
146 |
bits >>= 1; |
|
147 |
numBits++; |
|
148 |
} |
|
149 |
||
150 |
if (numBits >= (int)sizeof(prime_deltas)) { |
|
151 |
numBits = sizeof(prime_deltas) - 1; |
|
152 |
} else if (primeForNumBits(numBits) < hint) { |
|
153 |
++numBits; |
|
154 |
} |
|
155 |
return numBits; |
|
156 |
} |
|
157 |
||
158 |
/* |
|
159 |
A QHash has initially around pow(2, MinNumBits) buckets. For |
|
160 |
example, if MinNumBits is 4, it has 17 buckets. |
|
161 |
*/ |
|
162 |
const int MinNumBits = 4; |
|
163 |
||
164 |
QHashData QHashData::shared_null = { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
165 |
0, 0, Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, MinNumBits, 0, 0, true, false, 0 |
0 | 166 |
}; |
167 |
||
168 |
void *QHashData::allocateNode() |
|
169 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
return allocateNode(0); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
|
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
void *QHashData::allocateNode(int nodeAlign) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
{ |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
void *ptr = strictAlignment ? qMallocAligned(nodeSize, nodeAlign) : qMalloc(nodeSize); |
0 | 176 |
Q_CHECK_PTR(ptr); |
177 |
return ptr; |
|
178 |
} |
|
179 |
||
180 |
void QHashData::freeNode(void *node) |
|
181 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
if (strictAlignment) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
qFreeAligned(node); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
185 |
qFree(node); |
0 | 186 |
} |
187 |
||
188 |
QHashData *QHashData::detach_helper(void (*node_duplicate)(Node *, void *), int nodeSize) |
|
189 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
return detach_helper2( node_duplicate, 0, nodeSize, 0 ); |
0 | 191 |
} |
192 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
193 |
QHashData *QHashData::detach_helper2(void (*node_duplicate)(Node *, void *), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
void (*node_delete)(Node *), |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
195 |
int nodeSize, |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
196 |
int nodeAlign) |
0 | 197 |
{ |
198 |
union { |
|
199 |
QHashData *d; |
|
200 |
Node *e; |
|
201 |
}; |
|
202 |
d = new QHashData; |
|
203 |
d->fakeNext = 0; |
|
204 |
d->buckets = 0; |
|
205 |
d->ref = 1; |
|
206 |
d->size = size; |
|
207 |
d->nodeSize = nodeSize; |
|
208 |
d->userNumBits = userNumBits; |
|
209 |
d->numBits = numBits; |
|
210 |
d->numBuckets = numBuckets; |
|
211 |
d->sharable = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
d->strictAlignment = nodeAlign > 8; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
d->reserved = 0; |
0 | 214 |
|
215 |
if (numBuckets) { |
|
216 |
QT_TRY { |
|
217 |
d->buckets = new Node *[numBuckets]; |
|
218 |
} QT_CATCH(...) { |
|
219 |
// restore a consistent state for d |
|
220 |
d->numBuckets = 0; |
|
221 |
// roll back |
|
222 |
d->free_helper(node_delete); |
|
223 |
QT_RETHROW; |
|
224 |
} |
|
225 |
||
226 |
Node *this_e = reinterpret_cast<Node *>(this); |
|
227 |
for (int i = 0; i < numBuckets; ++i) { |
|
228 |
Node **nextNode = &d->buckets[i]; |
|
229 |
Node *oldNode = buckets[i]; |
|
230 |
while (oldNode != this_e) { |
|
231 |
QT_TRY { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
Node *dup = static_cast<Node *>(allocateNode(nodeAlign)); |
0 | 233 |
|
234 |
QT_TRY { |
|
235 |
node_duplicate(oldNode, dup); |
|
236 |
} QT_CATCH(...) { |
|
237 |
freeNode( dup ); |
|
238 |
QT_RETHROW; |
|
239 |
} |
|
240 |
||
241 |
dup->h = oldNode->h; |
|
242 |
*nextNode = dup; |
|
243 |
nextNode = &dup->next; |
|
244 |
oldNode = oldNode->next; |
|
245 |
} QT_CATCH(...) { |
|
246 |
// restore a consistent state for d |
|
247 |
*nextNode = e; |
|
248 |
d->numBuckets = i+1; |
|
249 |
// roll back |
|
250 |
d->free_helper(node_delete); |
|
251 |
QT_RETHROW; |
|
252 |
} |
|
253 |
} |
|
254 |
*nextNode = e; |
|
255 |
} |
|
256 |
} |
|
257 |
return d; |
|
258 |
} |
|
259 |
||
260 |
void QHashData::free_helper(void (*node_delete)(Node *)) |
|
261 |
{ |
|
262 |
if (node_delete) { |
|
263 |
Node *this_e = reinterpret_cast<Node *>(this); |
|
264 |
Node **bucket = reinterpret_cast<Node **>(this->buckets); |
|
265 |
||
266 |
int n = numBuckets; |
|
267 |
while (n--) { |
|
268 |
Node *cur = *bucket++; |
|
269 |
while (cur != this_e) { |
|
270 |
Node *next = cur->next; |
|
271 |
node_delete(cur); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
freeNode(cur); |
0 | 273 |
cur = next; |
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
delete [] buckets; |
|
278 |
delete this; |
|
279 |
} |
|
280 |
||
281 |
QHashData::Node *QHashData::nextNode(Node *node) |
|
282 |
{ |
|
283 |
union { |
|
284 |
Node *next; |
|
285 |
Node *e; |
|
286 |
QHashData *d; |
|
287 |
}; |
|
288 |
next = node->next; |
|
289 |
Q_ASSERT_X(next, "QHash", "Iterating beyond end()"); |
|
290 |
if (next->next) |
|
291 |
return next; |
|
292 |
||
293 |
int start = (node->h % d->numBuckets) + 1; |
|
294 |
Node **bucket = d->buckets + start; |
|
295 |
int n = d->numBuckets - start; |
|
296 |
while (n--) { |
|
297 |
if (*bucket != e) |
|
298 |
return *bucket; |
|
299 |
++bucket; |
|
300 |
} |
|
301 |
return e; |
|
302 |
} |
|
303 |
||
304 |
QHashData::Node *QHashData::previousNode(Node *node) |
|
305 |
{ |
|
306 |
union { |
|
307 |
Node *e; |
|
308 |
QHashData *d; |
|
309 |
}; |
|
310 |
||
311 |
e = node; |
|
312 |
while (e->next) |
|
313 |
e = e->next; |
|
314 |
||
315 |
int start; |
|
316 |
if (node == e) |
|
317 |
start = d->numBuckets - 1; |
|
318 |
else |
|
319 |
start = node->h % d->numBuckets; |
|
320 |
||
321 |
Node *sentinel = node; |
|
322 |
Node **bucket = d->buckets + start; |
|
323 |
while (start >= 0) { |
|
324 |
if (*bucket != sentinel) { |
|
325 |
Node *prev = *bucket; |
|
326 |
while (prev->next != sentinel) |
|
327 |
prev = prev->next; |
|
328 |
return prev; |
|
329 |
} |
|
330 |
||
331 |
sentinel = e; |
|
332 |
--bucket; |
|
333 |
--start; |
|
334 |
} |
|
335 |
Q_ASSERT_X(start >= 0, "QHash", "Iterating backward beyond begin()"); |
|
336 |
return e; |
|
337 |
} |
|
338 |
||
339 |
/* |
|
340 |
If hint is negative, -hint gives the approximate number of |
|
341 |
buckets that should be used for the hash table. If hint is |
|
342 |
nonnegative, (1 << hint) gives the approximate number |
|
343 |
of buckets that should be used. |
|
344 |
*/ |
|
345 |
void QHashData::rehash(int hint) |
|
346 |
{ |
|
347 |
if (hint < 0) { |
|
348 |
hint = countBits(-hint); |
|
349 |
if (hint < MinNumBits) |
|
350 |
hint = MinNumBits; |
|
351 |
userNumBits = hint; |
|
352 |
while (primeForNumBits(hint) < (size >> 1)) |
|
353 |
++hint; |
|
354 |
} else if (hint < MinNumBits) { |
|
355 |
hint = MinNumBits; |
|
356 |
} |
|
357 |
||
358 |
if (numBits != hint) { |
|
359 |
Node *e = reinterpret_cast<Node *>(this); |
|
360 |
Node **oldBuckets = buckets; |
|
361 |
int oldNumBuckets = numBuckets; |
|
362 |
||
363 |
int nb = primeForNumBits(hint); |
|
364 |
buckets = new Node *[nb]; |
|
365 |
numBits = hint; |
|
366 |
numBuckets = nb; |
|
367 |
for (int i = 0; i < numBuckets; ++i) |
|
368 |
buckets[i] = e; |
|
369 |
||
370 |
for (int i = 0; i < oldNumBuckets; ++i) { |
|
371 |
Node *firstNode = oldBuckets[i]; |
|
372 |
while (firstNode != e) { |
|
373 |
uint h = firstNode->h; |
|
374 |
Node *lastNode = firstNode; |
|
375 |
while (lastNode->next != e && lastNode->next->h == h) |
|
376 |
lastNode = lastNode->next; |
|
377 |
||
378 |
Node *afterLastNode = lastNode->next; |
|
379 |
Node **beforeFirstNode = &buckets[h % numBuckets]; |
|
380 |
while (*beforeFirstNode != e) |
|
381 |
beforeFirstNode = &(*beforeFirstNode)->next; |
|
382 |
lastNode->next = *beforeFirstNode; |
|
383 |
*beforeFirstNode = firstNode; |
|
384 |
firstNode = afterLastNode; |
|
385 |
} |
|
386 |
} |
|
387 |
delete [] oldBuckets; |
|
388 |
} |
|
389 |
} |
|
390 |
||
391 |
void QHashData::destroyAndFree() |
|
392 |
{ |
|
393 |
free_helper(0); |
|
394 |
} |
|
395 |
||
396 |
#ifdef QT_QHASH_DEBUG |
|
397 |
||
398 |
void QHashData::dump() |
|
399 |
{ |
|
400 |
qDebug("Hash data (ref = %d, size = %d, nodeSize = %d, userNumBits = %d, numBits = %d, numBuckets = %d)", |
|
401 |
int(ref), size, nodeSize, userNumBits, numBits, |
|
402 |
numBuckets); |
|
403 |
qDebug(" %p (fakeNode = %p)", this, fakeNext); |
|
404 |
for (int i = 0; i < numBuckets; ++i) { |
|
405 |
QString line; |
|
406 |
Node *n = buckets[i]; |
|
407 |
if (n != reinterpret_cast<Node *>(this)) { |
|
408 |
line.sprintf("%d:", i); |
|
409 |
while (n != reinterpret_cast<Node *>(this)) { |
|
410 |
line += QString().sprintf(" -> [%p]", n); |
|
411 |
if (!n) { |
|
412 |
line += " (CORRUPT)"; |
|
413 |
break; |
|
414 |
} |
|
415 |
n = n->next; |
|
416 |
} |
|
417 |
qDebug(qPrintable(line)); |
|
418 |
} |
|
419 |
} |
|
420 |
} |
|
421 |
||
422 |
void QHashData::checkSanity() |
|
423 |
{ |
|
424 |
if (fakeNext) |
|
425 |
qFatal("Fake next isn't 0"); |
|
426 |
||
427 |
for (int i = 0; i < numBuckets; ++i) { |
|
428 |
Node *n = buckets[i]; |
|
429 |
Node *p = n; |
|
430 |
if (!n) |
|
431 |
qFatal("%d: Bucket entry is 0", i); |
|
432 |
if (n != reinterpret_cast<Node *>(this)) { |
|
433 |
while (n != reinterpret_cast<Node *>(this)) { |
|
434 |
if (!n->next) |
|
435 |
qFatal("%d: Next of %p is 0, should be %p", i, n, this); |
|
436 |
n = n->next; |
|
437 |
} |
|
438 |
} |
|
439 |
} |
|
440 |
} |
|
441 |
#endif |
|
442 |
||
443 |
/*! |
|
444 |
\fn uint qHash(const QPair<T1, T2> &key) |
|
445 |
\since 4.3 |
|
446 |
\relates QHash |
|
447 |
||
448 |
Returns the hash value for the \a key. |
|
449 |
||
450 |
Types \c T1 and \c T2 must be supported by qHash(). |
|
451 |
*/ |
|
452 |
||
453 |
/*! \fn uint qHash(char key) |
|
454 |
\relates QHash |
|
455 |
||
456 |
Returns the hash value for the \a key. |
|
457 |
*/ |
|
458 |
||
459 |
/*! \fn uint qHash(uchar key) |
|
460 |
\relates QHash |
|
461 |
||
462 |
Returns the hash value for the \a key. |
|
463 |
*/ |
|
464 |
||
465 |
/*! \fn uint qHash(signed char key) |
|
466 |
\relates QHash |
|
467 |
||
468 |
Returns the hash value for the \a key. |
|
469 |
*/ |
|
470 |
||
471 |
/*! \fn uint qHash(ushort key) |
|
472 |
\relates QHash |
|
473 |
||
474 |
Returns the hash value for the \a key. |
|
475 |
*/ |
|
476 |
||
477 |
/*! \fn uint qHash(short key) |
|
478 |
\relates QHash |
|
479 |
||
480 |
Returns the hash value for the \a key. |
|
481 |
*/ |
|
482 |
||
483 |
/*! \fn uint qHash(uint key) |
|
484 |
\relates QHash |
|
485 |
||
486 |
Returns the hash value for the \a key. |
|
487 |
*/ |
|
488 |
||
489 |
/*! \fn uint qHash(int key) |
|
490 |
\relates QHash |
|
491 |
||
492 |
Returns the hash value for the \a key. |
|
493 |
*/ |
|
494 |
||
495 |
/*! \fn uint qHash(ulong key) |
|
496 |
\relates QHash |
|
497 |
||
498 |
Returns the hash value for the \a key. |
|
499 |
*/ |
|
500 |
||
501 |
/*! \fn uint qHash(long key) |
|
502 |
\relates QHash |
|
503 |
||
504 |
Returns the hash value for the \a key. |
|
505 |
*/ |
|
506 |
||
507 |
/*! \fn uint qHash(quint64 key) |
|
508 |
\relates QHash |
|
509 |
||
510 |
Returns the hash value for the \a key. |
|
511 |
*/ |
|
512 |
||
513 |
/*! \fn uint qHash(qint64 key) |
|
514 |
\relates QHash |
|
515 |
||
516 |
Returns the hash value for the \a key. |
|
517 |
*/ |
|
518 |
||
519 |
/*! \fn uint qHash(QChar key) |
|
520 |
\relates QHash |
|
521 |
||
522 |
Returns the hash value for the \a key. |
|
523 |
*/ |
|
524 |
||
525 |
/*! \fn uint qHash(const QByteArray &key) |
|
526 |
\fn uint qHash(const QBitArray &key) |
|
527 |
\relates QHash |
|
528 |
||
529 |
Returns the hash value for the \a key. |
|
530 |
*/ |
|
531 |
||
532 |
/*! \fn uint qHash(const QString &key) |
|
533 |
\relates QHash |
|
534 |
||
535 |
Returns the hash value for the \a key. |
|
536 |
*/ |
|
537 |
||
538 |
/*! \fn uint qHash(const T *key) |
|
539 |
\relates QHash |
|
540 |
||
541 |
Returns the hash value for the \a key. |
|
542 |
*/ |
|
543 |
||
544 |
/*! |
|
545 |
\class QHash |
|
546 |
\brief The QHash class is a template class that provides a hash-table-based dictionary. |
|
547 |
||
548 |
\ingroup tools |
|
549 |
\ingroup shared |
|
550 |
||
551 |
\reentrant |
|
552 |
||
553 |
QHash\<Key, T\> is one of Qt's generic \l{container classes}. It |
|
554 |
stores (key, value) pairs and provides very fast lookup of the |
|
555 |
value associated with a key. |
|
556 |
||
557 |
QHash provides very similar functionality to QMap. The |
|
558 |
differences are: |
|
559 |
||
560 |
\list |
|
561 |
\i QHash provides faster lookups than QMap. (See \l{Algorithmic |
|
562 |
Complexity} for details.) |
|
563 |
\i When iterating over a QMap, the items are always sorted by |
|
564 |
key. With QHash, the items are arbitrarily ordered. |
|
565 |
\i The key type of a QMap must provide operator<(). The key |
|
566 |
type of a QHash must provide operator==() and a global |
|
567 |
hash function called qHash() (see the related non-member |
|
568 |
functions). |
|
569 |
\endlist |
|
570 |
||
571 |
Here's an example QHash with QString keys and \c int values: |
|
572 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 0 |
|
573 |
||
574 |
To insert a (key, value) pair into the hash, you can use operator[](): |
|
575 |
||
576 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 1 |
|
577 |
||
578 |
This inserts the following three (key, value) pairs into the |
|
579 |
QHash: ("one", 1), ("three", 3), and ("seven", 7). Another way to |
|
580 |
insert items into the hash is to use insert(): |
|
581 |
||
582 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 2 |
|
583 |
||
584 |
To look up a value, use operator[]() or value(): |
|
585 |
||
586 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 3 |
|
587 |
||
588 |
If there is no item with the specified key in the hash, these |
|
589 |
functions return a \l{default-constructed value}. |
|
590 |
||
591 |
If you want to check whether the hash contains a particular key, |
|
592 |
use contains(): |
|
593 |
||
594 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 4 |
|
595 |
||
596 |
There is also a value() overload that uses its second argument as |
|
597 |
a default value if there is no item with the specified key: |
|
598 |
||
599 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 5 |
|
600 |
||
601 |
In general, we recommend that you use contains() and value() |
|
602 |
rather than operator[]() for looking up a key in a hash. The |
|
603 |
reason is that operator[]() silently inserts an item into the |
|
604 |
hash if no item exists with the same key (unless the hash is |
|
605 |
const). For example, the following code snippet will create 1000 |
|
606 |
items in memory: |
|
607 |
||
608 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 6 |
|
609 |
||
610 |
To avoid this problem, replace \c hash[i] with \c hash.value(i) |
|
611 |
in the code above. |
|
612 |
||
613 |
If you want to navigate through all the (key, value) pairs stored |
|
614 |
in a QHash, you can use an iterator. QHash provides both |
|
615 |
\l{Java-style iterators} (QHashIterator and QMutableHashIterator) |
|
616 |
and \l{STL-style iterators} (QHash::const_iterator and |
|
617 |
QHash::iterator). Here's how to iterate over a QHash<QString, |
|
618 |
int> using a Java-style iterator: |
|
619 |
||
620 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 7 |
|
621 |
||
622 |
Here's the same code, but using an STL-style iterator: |
|
623 |
||
624 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 8 |
|
625 |
||
626 |
QHash is unordered, so an iterator's sequence cannot be assumed |
|
627 |
to be predictable. If ordering by key is required, use a QMap. |
|
628 |
||
629 |
Normally, a QHash allows only one value per key. If you call |
|
630 |
insert() with a key that already exists in the QHash, the |
|
631 |
previous value is erased. For example: |
|
632 |
||
633 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 9 |
|
634 |
||
635 |
However, you can store multiple values per key by using |
|
636 |
insertMulti() instead of insert() (or using the convenience |
|
637 |
subclass QMultiHash). If you want to retrieve all |
|
638 |
the values for a single key, you can use values(const Key &key), |
|
639 |
which returns a QList<T>: |
|
640 |
||
641 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 10 |
|
642 |
||
643 |
The items that share the same key are available from most |
|
644 |
recently to least recently inserted. A more efficient approach is |
|
645 |
to call find() to get the iterator for the first item with a key |
|
646 |
and iterate from there: |
|
647 |
||
648 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 11 |
|
649 |
||
650 |
If you only need to extract the values from a hash (not the keys), |
|
651 |
you can also use \l{foreach}: |
|
652 |
||
653 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 12 |
|
654 |
||
655 |
Items can be removed from the hash in several ways. One way is to |
|
656 |
call remove(); this will remove any item with the given key. |
|
657 |
Another way is to use QMutableHashIterator::remove(). In addition, |
|
658 |
you can clear the entire hash using clear(). |
|
659 |
||
660 |
QHash's key and value data types must be \l{assignable data |
|
661 |
types}. You cannot, for example, store a QWidget as a value; |
|
662 |
instead, store a QWidget *. In addition, QHash's key type must |
|
663 |
provide operator==(), and there must also be a global qHash() |
|
664 |
function that returns a hash value for an argument of the key's |
|
665 |
type. |
|
666 |
||
667 |
Here's a list of the C++ and Qt types that can serve as keys in a |
|
668 |
QHash: any integer type (char, unsigned long, etc.), any pointer |
|
669 |
type, QChar, QString, and QByteArray. For all of these, the \c |
|
670 |
<QHash> header defines a qHash() function that computes an |
|
671 |
adequate hash value. If you want to use other types as the key, |
|
672 |
make sure that you provide operator==() and a qHash() |
|
673 |
implementation. |
|
674 |
||
675 |
Example: |
|
676 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 13 |
|
677 |
||
678 |
The qHash() function computes a numeric value based on a key. It |
|
679 |
can use any algorithm imaginable, as long as it always returns |
|
680 |
the same value if given the same argument. In other words, if |
|
681 |
\c{e1 == e2}, then \c{qHash(e1) == qHash(e2)} must hold as well. |
|
682 |
However, to obtain good performance, the qHash() function should |
|
683 |
attempt to return different hash values for different keys to the |
|
684 |
largest extent possible. |
|
685 |
||
686 |
In the example above, we've relied on Qt's global qHash(const |
|
687 |
QString &) to give us a hash value for the employee's name, and |
|
688 |
XOR'ed this with the day they were born to help produce unique |
|
689 |
hashes for people with the same name. |
|
690 |
||
691 |
Internally, QHash uses a hash table to perform lookups. Unlike Qt |
|
692 |
3's \c QDict class, which needed to be initialized with a prime |
|
693 |
number, QHash's hash table automatically grows and shrinks to |
|
694 |
provide fast lookups without wasting too much memory. You can |
|
695 |
still control the size of the hash table by calling reserve() if |
|
696 |
you already know approximately how many items the QHash will |
|
697 |
contain, but this isn't necessary to obtain good performance. You |
|
698 |
can also call capacity() to retrieve the hash table's size. |
|
699 |
||
700 |
\sa QHashIterator, QMutableHashIterator, QMap, QSet |
|
701 |
*/ |
|
702 |
||
703 |
/*! \fn QHash::QHash() |
|
704 |
||
705 |
Constructs an empty hash. |
|
706 |
||
707 |
\sa clear() |
|
708 |
*/ |
|
709 |
||
710 |
/*! \fn QHash::QHash(const QHash<Key, T> &other) |
|
711 |
||
712 |
Constructs a copy of \a other. |
|
713 |
||
714 |
This operation occurs in \l{constant time}, because QHash is |
|
715 |
\l{implicitly shared}. This makes returning a QHash from a |
|
716 |
function very fast. If a shared instance is modified, it will be |
|
717 |
copied (copy-on-write), and this takes \l{linear time}. |
|
718 |
||
719 |
\sa operator=() |
|
720 |
*/ |
|
721 |
||
722 |
/*! \fn QHash::~QHash() |
|
723 |
||
724 |
Destroys the hash. References to the values in the hash and all |
|
725 |
iterators of this hash become invalid. |
|
726 |
*/ |
|
727 |
||
728 |
/*! \fn QHash<Key, T> &QHash::operator=(const QHash<Key, T> &other) |
|
729 |
||
730 |
Assigns \a other to this hash and returns a reference to this hash. |
|
731 |
*/ |
|
732 |
||
733 |
/*! \fn bool QHash::operator==(const QHash<Key, T> &other) const |
|
734 |
||
735 |
Returns true if \a other is equal to this hash; otherwise returns |
|
736 |
false. |
|
737 |
||
738 |
Two hashes are considered equal if they contain the same (key, |
|
739 |
value) pairs. |
|
740 |
||
741 |
This function requires the value type to implement \c operator==(). |
|
742 |
||
743 |
\sa operator!=() |
|
744 |
*/ |
|
745 |
||
746 |
/*! \fn bool QHash::operator!=(const QHash<Key, T> &other) const |
|
747 |
||
748 |
Returns true if \a other is not equal to this hash; otherwise |
|
749 |
returns false. |
|
750 |
||
751 |
Two hashes are considered equal if they contain the same (key, |
|
752 |
value) pairs. |
|
753 |
||
754 |
This function requires the value type to implement \c operator==(). |
|
755 |
||
756 |
\sa operator==() |
|
757 |
*/ |
|
758 |
||
759 |
/*! \fn int QHash::size() const |
|
760 |
||
761 |
Returns the number of items in the hash. |
|
762 |
||
763 |
\sa isEmpty(), count() |
|
764 |
*/ |
|
765 |
||
766 |
/*! \fn bool QHash::isEmpty() const |
|
767 |
||
768 |
Returns true if the hash contains no items; otherwise returns |
|
769 |
false. |
|
770 |
||
771 |
\sa size() |
|
772 |
*/ |
|
773 |
||
774 |
/*! \fn int QHash::capacity() const |
|
775 |
||
776 |
Returns the number of buckets in the QHash's internal hash table. |
|
777 |
||
778 |
The sole purpose of this function is to provide a means of fine |
|
779 |
tuning QHash's memory usage. In general, you will rarely ever |
|
780 |
need to call this function. If you want to know how many items are |
|
781 |
in the hash, call size(). |
|
782 |
||
783 |
\sa reserve(), squeeze() |
|
784 |
*/ |
|
785 |
||
786 |
/*! \fn void QHash::reserve(int size) |
|
787 |
||
788 |
Ensures that the QHash's internal hash table consists of at least |
|
789 |
\a size buckets. |
|
790 |
||
791 |
This function is useful for code that needs to build a huge hash |
|
792 |
and wants to avoid repeated reallocation. For example: |
|
793 |
||
794 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 14 |
|
795 |
||
796 |
Ideally, \a size should be slightly more than the maximum number |
|
797 |
of items expected in the hash. \a size doesn't have to be prime, |
|
798 |
because QHash will use a prime number internally anyway. If \a size |
|
799 |
is an underestimate, the worst that will happen is that the QHash |
|
800 |
will be a bit slower. |
|
801 |
||
802 |
In general, you will rarely ever need to call this function. |
|
803 |
QHash's internal hash table automatically shrinks or grows to |
|
804 |
provide good performance without wasting too much memory. |
|
805 |
||
806 |
\sa squeeze(), capacity() |
|
807 |
*/ |
|
808 |
||
809 |
/*! \fn void QHash::squeeze() |
|
810 |
||
811 |
Reduces the size of the QHash's internal hash table to save |
|
812 |
memory. |
|
813 |
||
814 |
The sole purpose of this function is to provide a means of fine |
|
815 |
tuning QHash's memory usage. In general, you will rarely ever |
|
816 |
need to call this function. |
|
817 |
||
818 |
\sa reserve(), capacity() |
|
819 |
*/ |
|
820 |
||
821 |
/*! \fn void QHash::detach() |
|
822 |
||
823 |
\internal |
|
824 |
||
825 |
Detaches this hash from any other hashes with which it may share |
|
826 |
data. |
|
827 |
||
828 |
\sa isDetached() |
|
829 |
*/ |
|
830 |
||
831 |
/*! \fn bool QHash::isDetached() const |
|
832 |
||
833 |
\internal |
|
834 |
||
835 |
Returns true if the hash's internal data isn't shared with any |
|
836 |
other hash object; otherwise returns false. |
|
837 |
||
838 |
\sa detach() |
|
839 |
*/ |
|
840 |
||
841 |
/*! \fn void QHash::setSharable(bool sharable) |
|
842 |
||
843 |
\internal |
|
844 |
*/ |
|
845 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
846 |
/*! \fn bool QHash::isSharedWith(const QHash<Key, T> &other) const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
847 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
848 |
\internal |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
849 |
*/ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
850 |
|
0 | 851 |
/*! \fn void QHash::clear() |
852 |
||
853 |
Removes all items from the hash. |
|
854 |
||
855 |
\sa remove() |
|
856 |
*/ |
|
857 |
||
858 |
/*! \fn int QHash::remove(const Key &key) |
|
859 |
||
860 |
Removes all the items that have the \a key from the hash. |
|
861 |
Returns the number of items removed which is usually 1 but will |
|
862 |
be 0 if the key isn't in the hash, or greater than 1 if |
|
863 |
insertMulti() has been used with the \a key. |
|
864 |
||
865 |
\sa clear(), take(), QMultiHash::remove() |
|
866 |
*/ |
|
867 |
||
868 |
/*! \fn T QHash::take(const Key &key) |
|
869 |
||
870 |
Removes the item with the \a key from the hash and returns |
|
871 |
the value associated with it. |
|
872 |
||
873 |
If the item does not exist in the hash, the function simply |
|
874 |
returns a \l{default-constructed value}. If there are multiple |
|
875 |
items for \a key in the hash, only the most recently inserted one |
|
876 |
is removed. |
|
877 |
||
878 |
If you don't use the return value, remove() is more efficient. |
|
879 |
||
880 |
\sa remove() |
|
881 |
*/ |
|
882 |
||
883 |
/*! \fn bool QHash::contains(const Key &key) const |
|
884 |
||
885 |
Returns true if the hash contains an item with the \a key; |
|
886 |
otherwise returns false. |
|
887 |
||
888 |
\sa count(), QMultiHash::contains() |
|
889 |
*/ |
|
890 |
||
891 |
/*! \fn const T QHash::value(const Key &key) const |
|
892 |
||
893 |
Returns the value associated with the \a key. |
|
894 |
||
895 |
If the hash contains no item with the \a key, the function |
|
896 |
returns a \l{default-constructed value}. If there are multiple |
|
897 |
items for the \a key in the hash, the value of the most recently |
|
898 |
inserted one is returned. |
|
899 |
||
900 |
\sa key(), values(), contains(), operator[]() |
|
901 |
*/ |
|
902 |
||
903 |
/*! \fn const T QHash::value(const Key &key, const T &defaultValue) const |
|
904 |
\overload |
|
905 |
||
906 |
If the hash contains no item with the given \a key, the function returns |
|
907 |
\a defaultValue. |
|
908 |
*/ |
|
909 |
||
910 |
/*! \fn T &QHash::operator[](const Key &key) |
|
911 |
||
912 |
Returns the value associated with the \a key as a modifiable |
|
913 |
reference. |
|
914 |
||
915 |
If the hash contains no item with the \a key, the function inserts |
|
916 |
a \l{default-constructed value} into the hash with the \a key, and |
|
917 |
returns a reference to it. If the hash contains multiple items |
|
918 |
with the \a key, this function returns a reference to the most |
|
919 |
recently inserted value. |
|
920 |
||
921 |
\sa insert(), value() |
|
922 |
*/ |
|
923 |
||
924 |
/*! \fn const T QHash::operator[](const Key &key) const |
|
925 |
||
926 |
\overload |
|
927 |
||
928 |
Same as value(). |
|
929 |
*/ |
|
930 |
||
931 |
/*! \fn QList<Key> QHash::uniqueKeys() const |
|
932 |
\since 4.2 |
|
933 |
||
934 |
Returns a list containing all the keys in the map. Keys that occur multiple |
|
935 |
times in the map (because items were inserted with insertMulti(), or |
|
936 |
unite() was used) occur only once in the returned list. |
|
937 |
||
938 |
\sa keys(), values() |
|
939 |
*/ |
|
940 |
||
941 |
/*! \fn QList<Key> QHash::keys() const |
|
942 |
||
943 |
Returns a list containing all the keys in the hash, in an |
|
944 |
arbitrary order. Keys that occur multiple times in the hash |
|
945 |
(because items were inserted with insertMulti(), or unite() was |
|
946 |
used) also occur multiple times in the list. |
|
947 |
||
948 |
To obtain a list of unique keys, where each key from the map only |
|
949 |
occurs once, use uniqueKeys(). |
|
950 |
||
951 |
The order is guaranteed to be the same as that used by values(). |
|
952 |
||
953 |
\sa uniqueKeys(), values(), key() |
|
954 |
*/ |
|
955 |
||
956 |
/*! \fn QList<Key> QHash::keys(const T &value) const |
|
957 |
||
958 |
\overload |
|
959 |
||
960 |
Returns a list containing all the keys associated with value \a |
|
961 |
value, in an arbitrary order. |
|
962 |
||
963 |
This function can be slow (\l{linear time}), because QHash's |
|
964 |
internal data structure is optimized for fast lookup by key, not |
|
965 |
by value. |
|
966 |
*/ |
|
967 |
||
968 |
/*! \fn QList<T> QHash::values() const |
|
969 |
||
970 |
Returns a list containing all the values in the hash, in an |
|
971 |
arbitrary order. If a key is associated multiple values, all of |
|
972 |
its values will be in the list, and not just the most recently |
|
973 |
inserted one. |
|
974 |
||
975 |
The order is guaranteed to be the same as that used by keys(). |
|
976 |
||
977 |
\sa keys(), value() |
|
978 |
*/ |
|
979 |
||
980 |
/*! \fn QList<T> QHash::values(const Key &key) const |
|
981 |
||
982 |
\overload |
|
983 |
||
984 |
Returns a list of all the values associated with the \a key, |
|
985 |
from the most recently inserted to the least recently inserted. |
|
986 |
||
987 |
\sa count(), insertMulti() |
|
988 |
*/ |
|
989 |
||
990 |
/*! \fn Key QHash::key(const T &value) const |
|
991 |
||
992 |
Returns the first key mapped to \a value. |
|
993 |
||
994 |
If the hash contains no item with the \a value, the function |
|
995 |
returns a \link {default-constructed value} default-constructed |
|
996 |
key \endlink. |
|
997 |
||
998 |
This function can be slow (\l{linear time}), because QHash's |
|
999 |
internal data structure is optimized for fast lookup by key, not |
|
1000 |
by value. |
|
1001 |
||
1002 |
\sa value(), keys() |
|
1003 |
*/ |
|
1004 |
||
1005 |
/*! |
|
1006 |
\fn Key QHash::key(const T &value, const Key &defaultKey) const |
|
1007 |
\since 4.3 |
|
1008 |
\overload |
|
1009 |
||
1010 |
Returns the first key mapped to \a value, or \a defaultKey if the |
|
1011 |
hash contains no item mapped to \a value. |
|
1012 |
||
1013 |
This function can be slow (\l{linear time}), because QHash's |
|
1014 |
internal data structure is optimized for fast lookup by key, not |
|
1015 |
by value. |
|
1016 |
*/ |
|
1017 |
||
1018 |
/*! \fn int QHash::count(const Key &key) const |
|
1019 |
||
1020 |
Returns the number of items associated with the \a key. |
|
1021 |
||
1022 |
\sa contains(), insertMulti() |
|
1023 |
*/ |
|
1024 |
||
1025 |
/*! \fn int QHash::count() const |
|
1026 |
||
1027 |
\overload |
|
1028 |
||
1029 |
Same as size(). |
|
1030 |
*/ |
|
1031 |
||
1032 |
/*! \fn QHash::iterator QHash::begin() |
|
1033 |
||
1034 |
Returns an \l{STL-style iterator} pointing to the first item in |
|
1035 |
the hash. |
|
1036 |
||
1037 |
\sa constBegin(), end() |
|
1038 |
*/ |
|
1039 |
||
1040 |
/*! \fn QHash::const_iterator QHash::begin() const |
|
1041 |
||
1042 |
\overload |
|
1043 |
*/ |
|
1044 |
||
1045 |
/*! \fn QHash::const_iterator QHash::constBegin() const |
|
1046 |
||
1047 |
Returns a const \l{STL-style iterator} pointing to the first item |
|
1048 |
in the hash. |
|
1049 |
||
1050 |
\sa begin(), constEnd() |
|
1051 |
*/ |
|
1052 |
||
1053 |
/*! \fn QHash::iterator QHash::end() |
|
1054 |
||
1055 |
Returns an \l{STL-style iterator} pointing to the imaginary item |
|
1056 |
after the last item in the hash. |
|
1057 |
||
1058 |
\sa begin(), constEnd() |
|
1059 |
*/ |
|
1060 |
||
1061 |
/*! \fn QHash::const_iterator QHash::end() const |
|
1062 |
||
1063 |
\overload |
|
1064 |
*/ |
|
1065 |
||
1066 |
/*! \fn QHash::const_iterator QHash::constEnd() const |
|
1067 |
||
1068 |
Returns a const \l{STL-style iterator} pointing to the imaginary |
|
1069 |
item after the last item in the hash. |
|
1070 |
||
1071 |
\sa constBegin(), end() |
|
1072 |
*/ |
|
1073 |
||
1074 |
/*! \fn QHash::iterator QHash::erase(iterator pos) |
|
1075 |
||
1076 |
Removes the (key, value) pair associated with the iterator \a pos |
|
1077 |
from the hash, and returns an iterator to the next item in the |
|
1078 |
hash. |
|
1079 |
||
1080 |
Unlike remove() and take(), this function never causes QHash to |
|
1081 |
rehash its internal data structure. This means that it can safely |
|
1082 |
be called while iterating, and won't affect the order of items in |
|
1083 |
the hash. For example: |
|
1084 |
||
1085 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 15 |
|
1086 |
||
1087 |
\sa remove(), take(), find() |
|
1088 |
*/ |
|
1089 |
||
1090 |
/*! \fn QHash::iterator QHash::find(const Key &key) |
|
1091 |
||
1092 |
Returns an iterator pointing to the item with the \a key in the |
|
1093 |
hash. |
|
1094 |
||
1095 |
If the hash contains no item with the \a key, the function |
|
1096 |
returns end(). |
|
1097 |
||
1098 |
If the hash contains multiple items with the \a key, this |
|
1099 |
function returns an iterator that points to the most recently |
|
1100 |
inserted value. The other values are accessible by incrementing |
|
1101 |
the iterator. For example, here's some code that iterates over all |
|
1102 |
the items with the same key: |
|
1103 |
||
1104 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 16 |
|
1105 |
||
1106 |
\sa value(), values(), QMultiHash::find() |
|
1107 |
*/ |
|
1108 |
||
1109 |
/*! \fn QHash::const_iterator QHash::find(const Key &key) const |
|
1110 |
||
1111 |
\overload |
|
1112 |
*/ |
|
1113 |
||
1114 |
/*! \fn QHash::iterator QHash::constFind(const Key &key) const |
|
1115 |
\since 4.1 |
|
1116 |
||
1117 |
Returns an iterator pointing to the item with the \a key in the |
|
1118 |
hash. |
|
1119 |
||
1120 |
If the hash contains no item with the \a key, the function |
|
1121 |
returns constEnd(). |
|
1122 |
||
1123 |
\sa find(), QMultiHash::constFind() |
|
1124 |
*/ |
|
1125 |
||
1126 |
/*! \fn QHash::iterator QHash::insert(const Key &key, const T &value) |
|
1127 |
||
1128 |
Inserts a new item with the \a key and a value of \a value. |
|
1129 |
||
1130 |
If there is already an item with the \a key, that item's value |
|
1131 |
is replaced with \a value. |
|
1132 |
||
1133 |
If there are multiple items with the \a key, the most |
|
1134 |
recently inserted item's value is replaced with \a value. |
|
1135 |
||
1136 |
\sa insertMulti() |
|
1137 |
*/ |
|
1138 |
||
1139 |
/*! \fn QHash::iterator QHash::insertMulti(const Key &key, const T &value) |
|
1140 |
||
1141 |
Inserts a new item with the \a key and a value of \a value. |
|
1142 |
||
1143 |
If there is already an item with the same key in the hash, this |
|
1144 |
function will simply create a new one. (This behavior is |
|
1145 |
different from insert(), which overwrites the value of an |
|
1146 |
existing item.) |
|
1147 |
||
1148 |
\sa insert(), values() |
|
1149 |
*/ |
|
1150 |
||
1151 |
/*! \fn QHash<Key, T> &QHash::unite(const QHash<Key, T> &other) |
|
1152 |
||
1153 |
Inserts all the items in the \a other hash into this hash. If a |
|
1154 |
key is common to both hashes, the resulting hash will contain the |
|
1155 |
key multiple times. |
|
1156 |
||
1157 |
\sa insertMulti() |
|
1158 |
*/ |
|
1159 |
||
1160 |
/*! \fn bool QHash::empty() const |
|
1161 |
||
1162 |
This function is provided for STL compatibility. It is equivalent |
|
1163 |
to isEmpty(), returning true if the hash is empty; otherwise |
|
1164 |
returns false. |
|
1165 |
*/ |
|
1166 |
||
1167 |
/*! \typedef QHash::ConstIterator |
|
1168 |
||
1169 |
Qt-style synonym for QHash::const_iterator. |
|
1170 |
*/ |
|
1171 |
||
1172 |
/*! \typedef QHash::Iterator |
|
1173 |
||
1174 |
Qt-style synonym for QHash::iterator. |
|
1175 |
*/ |
|
1176 |
||
1177 |
/*! \typedef QHash::difference_type |
|
1178 |
||
1179 |
Typedef for ptrdiff_t. Provided for STL compatibility. |
|
1180 |
*/ |
|
1181 |
||
1182 |
/*! \typedef QHash::key_type |
|
1183 |
||
1184 |
Typedef for Key. Provided for STL compatibility. |
|
1185 |
*/ |
|
1186 |
||
1187 |
/*! \typedef QHash::mapped_type |
|
1188 |
||
1189 |
Typedef for T. Provided for STL compatibility. |
|
1190 |
*/ |
|
1191 |
||
1192 |
/*! \typedef QHash::size_type |
|
1193 |
||
1194 |
Typedef for int. Provided for STL compatibility. |
|
1195 |
*/ |
|
1196 |
||
1197 |
/*! \typedef QHash::iterator::difference_type |
|
1198 |
\internal |
|
1199 |
*/ |
|
1200 |
||
1201 |
/*! \typedef QHash::iterator::iterator_category |
|
1202 |
\internal |
|
1203 |
*/ |
|
1204 |
||
1205 |
/*! \typedef QHash::iterator::pointer |
|
1206 |
\internal |
|
1207 |
*/ |
|
1208 |
||
1209 |
/*! \typedef QHash::iterator::reference |
|
1210 |
\internal |
|
1211 |
*/ |
|
1212 |
||
1213 |
/*! \typedef QHash::iterator::value_type |
|
1214 |
\internal |
|
1215 |
*/ |
|
1216 |
||
1217 |
/*! \typedef QHash::const_iterator::difference_type |
|
1218 |
\internal |
|
1219 |
*/ |
|
1220 |
||
1221 |
/*! \typedef QHash::const_iterator::iterator_category |
|
1222 |
\internal |
|
1223 |
*/ |
|
1224 |
||
1225 |
/*! \typedef QHash::const_iterator::pointer |
|
1226 |
\internal |
|
1227 |
*/ |
|
1228 |
||
1229 |
/*! \typedef QHash::const_iterator::reference |
|
1230 |
\internal |
|
1231 |
*/ |
|
1232 |
||
1233 |
/*! \typedef QHash::const_iterator::value_type |
|
1234 |
\internal |
|
1235 |
*/ |
|
1236 |
||
1237 |
/*! \class QHash::iterator |
|
1238 |
\brief The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash. |
|
1239 |
||
1240 |
QHash features both \l{STL-style iterators} and \l{Java-style |
|
1241 |
iterators}. The STL-style iterators are more low-level and more |
|
1242 |
cumbersome to use; on the other hand, they are slightly faster |
|
1243 |
and, for developers who already know STL, have the advantage of |
|
1244 |
familiarity. |
|
1245 |
||
1246 |
QHash\<Key, T\>::iterator allows you to iterate over a QHash (or |
|
1247 |
QMultiHash) and to modify the value (but not the key) associated |
|
1248 |
with a particular key. If you want to iterate over a const QHash, |
|
1249 |
you should use QHash::const_iterator. It is generally good |
|
1250 |
practice to use QHash::const_iterator on a non-const QHash as |
|
1251 |
well, unless you need to change the QHash through the iterator. |
|
1252 |
Const iterators are slightly faster, and can improve code |
|
1253 |
readability. |
|
1254 |
||
1255 |
The default QHash::iterator constructor creates an uninitialized |
|
1256 |
iterator. You must initialize it using a QHash function like |
|
1257 |
QHash::begin(), QHash::end(), or QHash::find() before you can |
|
1258 |
start iterating. Here's a typical loop that prints all the (key, |
|
1259 |
value) pairs stored in a hash: |
|
1260 |
||
1261 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 17 |
|
1262 |
||
1263 |
Unlike QMap, which orders its items by key, QHash stores its |
|
1264 |
items in an arbitrary order. The only guarantee is that items that |
|
1265 |
share the same key (because they were inserted using |
|
1266 |
QHash::insertMulti()) will appear consecutively, from the most |
|
1267 |
recently to the least recently inserted value. |
|
1268 |
||
1269 |
Let's see a few examples of things we can do with a |
|
1270 |
QHash::iterator that we cannot do with a QHash::const_iterator. |
|
1271 |
Here's an example that increments every value stored in the QHash |
|
1272 |
by 2: |
|
1273 |
||
1274 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 18 |
|
1275 |
||
1276 |
Here's an example that removes all the items whose key is a |
|
1277 |
string that starts with an underscore character: |
|
1278 |
||
1279 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 19 |
|
1280 |
||
1281 |
The call to QHash::erase() removes the item pointed to by the |
|
1282 |
iterator from the hash, and returns an iterator to the next item. |
|
1283 |
Here's another way of removing an item while iterating: |
|
1284 |
||
1285 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 20 |
|
1286 |
||
1287 |
It might be tempting to write code like this: |
|
1288 |
||
1289 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 21 |
|
1290 |
||
1291 |
However, this will potentially crash in \c{++i}, because \c i is |
|
1292 |
a dangling iterator after the call to erase(). |
|
1293 |
||
1294 |
Multiple iterators can be used on the same hash. However, be |
|
1295 |
aware that any modification performed directly on the QHash has |
|
1296 |
the potential of dramatically changing the order in which the |
|
1297 |
items are stored in the hash, as they might cause QHash to rehash |
|
1298 |
its internal data structure. There is one notable exception: |
|
1299 |
QHash::erase(). This function can safely be called while |
|
1300 |
iterating, and won't affect the order of items in the hash. If you |
|
1301 |
need to keep iterators over a long period of time, we recommend |
|
1302 |
that you use QMap rather than QHash. |
|
1303 |
||
1304 |
\sa QHash::const_iterator, QMutableHashIterator |
|
1305 |
*/ |
|
1306 |
||
1307 |
/*! \fn QHash::iterator::operator Node *() const |
|
1308 |
||
1309 |
\internal |
|
1310 |
*/ |
|
1311 |
||
1312 |
/*! \fn QHash::iterator::iterator() |
|
1313 |
||
1314 |
Constructs an uninitialized iterator. |
|
1315 |
||
1316 |
Functions like key(), value(), and operator++() must not be |
|
1317 |
called on an uninitialized iterator. Use operator=() to assign a |
|
1318 |
value to it before using it. |
|
1319 |
||
1320 |
\sa QHash::begin() QHash::end() |
|
1321 |
*/ |
|
1322 |
||
1323 |
/*! \fn QHash::iterator::iterator(void *node) |
|
1324 |
||
1325 |
\internal |
|
1326 |
*/ |
|
1327 |
||
1328 |
/*! \fn const Key &QHash::iterator::key() const |
|
1329 |
||
1330 |
Returns the current item's key as a const reference. |
|
1331 |
||
1332 |
There is no direct way of changing an item's key through an |
|
1333 |
iterator, although it can be done by calling QHash::erase() |
|
1334 |
followed by QHash::insert() or QHash::insertMulti(). |
|
1335 |
||
1336 |
\sa value() |
|
1337 |
*/ |
|
1338 |
||
1339 |
/*! \fn T &QHash::iterator::value() const |
|
1340 |
||
1341 |
Returns a modifiable reference to the current item's value. |
|
1342 |
||
1343 |
You can change the value of an item by using value() on |
|
1344 |
the left side of an assignment, for example: |
|
1345 |
||
1346 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 22 |
|
1347 |
||
1348 |
\sa key(), operator*() |
|
1349 |
*/ |
|
1350 |
||
1351 |
/*! \fn T &QHash::iterator::operator*() const |
|
1352 |
||
1353 |
Returns a modifiable reference to the current item's value. |
|
1354 |
||
1355 |
Same as value(). |
|
1356 |
||
1357 |
\sa key() |
|
1358 |
*/ |
|
1359 |
||
1360 |
/*! \fn T *QHash::iterator::operator->() const |
|
1361 |
||
1362 |
Returns a pointer to the current item's value. |
|
1363 |
||
1364 |
\sa value() |
|
1365 |
*/ |
|
1366 |
||
1367 |
/*! |
|
1368 |
\fn bool QHash::iterator::operator==(const iterator &other) const |
|
1369 |
\fn bool QHash::iterator::operator==(const const_iterator &other) const |
|
1370 |
||
1371 |
Returns true if \a other points to the same item as this |
|
1372 |
iterator; otherwise returns false. |
|
1373 |
||
1374 |
\sa operator!=() |
|
1375 |
*/ |
|
1376 |
||
1377 |
/*! |
|
1378 |
\fn bool QHash::iterator::operator!=(const iterator &other) const |
|
1379 |
\fn bool QHash::iterator::operator!=(const const_iterator &other) const |
|
1380 |
||
1381 |
Returns true if \a other points to a different item than this |
|
1382 |
iterator; otherwise returns false. |
|
1383 |
||
1384 |
\sa operator==() |
|
1385 |
*/ |
|
1386 |
||
1387 |
/*! |
|
1388 |
\fn QHash::iterator &QHash::iterator::operator++() |
|
1389 |
||
1390 |
The prefix ++ operator (\c{++i}) advances the iterator to the |
|
1391 |
next item in the hash and returns an iterator to the new current |
|
1392 |
item. |
|
1393 |
||
1394 |
Calling this function on QHash::end() leads to undefined results. |
|
1395 |
||
1396 |
\sa operator--() |
|
1397 |
*/ |
|
1398 |
||
1399 |
/*! \fn QHash::iterator QHash::iterator::operator++(int) |
|
1400 |
||
1401 |
\overload |
|
1402 |
||
1403 |
The postfix ++ operator (\c{i++}) advances the iterator to the |
|
1404 |
next item in the hash and returns an iterator to the previously |
|
1405 |
current item. |
|
1406 |
*/ |
|
1407 |
||
1408 |
/*! |
|
1409 |
\fn QHash::iterator &QHash::iterator::operator--() |
|
1410 |
||
1411 |
The prefix -- operator (\c{--i}) makes the preceding item |
|
1412 |
current and returns an iterator pointing to the new current item. |
|
1413 |
||
1414 |
Calling this function on QHash::begin() leads to undefined |
|
1415 |
results. |
|
1416 |
||
1417 |
\sa operator++() |
|
1418 |
*/ |
|
1419 |
||
1420 |
/*! |
|
1421 |
\fn QHash::iterator QHash::iterator::operator--(int) |
|
1422 |
||
1423 |
\overload |
|
1424 |
||
1425 |
The postfix -- operator (\c{i--}) makes the preceding item |
|
1426 |
current and returns an iterator pointing to the previously |
|
1427 |
current item. |
|
1428 |
*/ |
|
1429 |
||
1430 |
/*! \fn QHash::iterator QHash::iterator::operator+(int j) const |
|
1431 |
||
1432 |
Returns an iterator to the item at \a j positions forward from |
|
1433 |
this iterator. (If \a j is negative, the iterator goes backward.) |
|
1434 |
||
1435 |
This operation can be slow for large \a j values. |
|
1436 |
||
1437 |
\sa operator-() |
|
1438 |
||
1439 |
*/ |
|
1440 |
||
1441 |
/*! \fn QHash::iterator QHash::iterator::operator-(int j) const |
|
1442 |
||
1443 |
Returns an iterator to the item at \a j positions backward from |
|
1444 |
this iterator. (If \a j is negative, the iterator goes forward.) |
|
1445 |
||
1446 |
This operation can be slow for large \a j values. |
|
1447 |
||
1448 |
\sa operator+() |
|
1449 |
*/ |
|
1450 |
||
1451 |
/*! \fn QHash::iterator &QHash::iterator::operator+=(int j) |
|
1452 |
||
1453 |
Advances the iterator by \a j items. (If \a j is negative, the |
|
1454 |
iterator goes backward.) |
|
1455 |
||
1456 |
\sa operator-=(), operator+() |
|
1457 |
*/ |
|
1458 |
||
1459 |
/*! \fn QHash::iterator &QHash::iterator::operator-=(int j) |
|
1460 |
||
1461 |
Makes the iterator go back by \a j items. (If \a j is negative, |
|
1462 |
the iterator goes forward.) |
|
1463 |
||
1464 |
\sa operator+=(), operator-() |
|
1465 |
*/ |
|
1466 |
||
1467 |
/*! \class QHash::const_iterator |
|
1468 |
\brief The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash. |
|
1469 |
||
1470 |
QHash features both \l{STL-style iterators} and \l{Java-style |
|
1471 |
iterators}. The STL-style iterators are more low-level and more |
|
1472 |
cumbersome to use; on the other hand, they are slightly faster |
|
1473 |
and, for developers who already know STL, have the advantage of |
|
1474 |
familiarity. |
|
1475 |
||
1476 |
QHash\<Key, T\>::const_iterator allows you to iterate over a |
|
1477 |
QHash (or a QMultiHash). If you want to modify the QHash as you |
|
1478 |
iterate over it, you must use QHash::iterator instead. It is |
|
1479 |
generally good practice to use QHash::const_iterator on a |
|
1480 |
non-const QHash as well, unless you need to change the QHash |
|
1481 |
through the iterator. Const iterators are slightly faster, and |
|
1482 |
can improve code readability. |
|
1483 |
||
1484 |
The default QHash::const_iterator constructor creates an |
|
1485 |
uninitialized iterator. You must initialize it using a QHash |
|
1486 |
function like QHash::constBegin(), QHash::constEnd(), or |
|
1487 |
QHash::find() before you can start iterating. Here's a typical |
|
1488 |
loop that prints all the (key, value) pairs stored in a hash: |
|
1489 |
||
1490 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 23 |
|
1491 |
||
1492 |
Unlike QMap, which orders its items by key, QHash stores its |
|
1493 |
items in an arbitrary order. The only guarantee is that items that |
|
1494 |
share the same key (because they were inserted using |
|
1495 |
QHash::insertMulti()) will appear consecutively, from the most |
|
1496 |
recently to the least recently inserted value. |
|
1497 |
||
1498 |
Multiple iterators can be used on the same hash. However, be aware |
|
1499 |
that any modification performed directly on the QHash has the |
|
1500 |
potential of dramatically changing the order in which the items |
|
1501 |
are stored in the hash, as they might cause QHash to rehash its |
|
1502 |
internal data structure. If you need to keep iterators over a long |
|
1503 |
period of time, we recommend that you use QMap rather than QHash. |
|
1504 |
||
1505 |
\sa QHash::iterator, QHashIterator |
|
1506 |
*/ |
|
1507 |
||
1508 |
/*! \fn QHash::const_iterator::operator Node *() const |
|
1509 |
||
1510 |
\internal |
|
1511 |
*/ |
|
1512 |
||
1513 |
/*! \fn QHash::const_iterator::const_iterator() |
|
1514 |
||
1515 |
Constructs an uninitialized iterator. |
|
1516 |
||
1517 |
Functions like key(), value(), and operator++() must not be |
|
1518 |
called on an uninitialized iterator. Use operator=() to assign a |
|
1519 |
value to it before using it. |
|
1520 |
||
1521 |
\sa QHash::constBegin() QHash::constEnd() |
|
1522 |
*/ |
|
1523 |
||
1524 |
/*! \fn QHash::const_iterator::const_iterator(void *node) |
|
1525 |
||
1526 |
\internal |
|
1527 |
*/ |
|
1528 |
||
1529 |
/*! \fn QHash::const_iterator::const_iterator(const iterator &other) |
|
1530 |
||
1531 |
Constructs a copy of \a other. |
|
1532 |
*/ |
|
1533 |
||
1534 |
/*! \fn const Key &QHash::const_iterator::key() const |
|
1535 |
||
1536 |
Returns the current item's key. |
|
1537 |
||
1538 |
\sa value() |
|
1539 |
*/ |
|
1540 |
||
1541 |
/*! \fn const T &QHash::const_iterator::value() const |
|
1542 |
||
1543 |
Returns the current item's value. |
|
1544 |
||
1545 |
\sa key(), operator*() |
|
1546 |
*/ |
|
1547 |
||
1548 |
/*! \fn const T &QHash::const_iterator::operator*() const |
|
1549 |
||
1550 |
Returns the current item's value. |
|
1551 |
||
1552 |
Same as value(). |
|
1553 |
||
1554 |
\sa key() |
|
1555 |
*/ |
|
1556 |
||
1557 |
/*! \fn const T *QHash::const_iterator::operator->() const |
|
1558 |
||
1559 |
Returns a pointer to the current item's value. |
|
1560 |
||
1561 |
\sa value() |
|
1562 |
*/ |
|
1563 |
||
1564 |
/*! \fn bool QHash::const_iterator::operator==(const const_iterator &other) const |
|
1565 |
||
1566 |
Returns true if \a other points to the same item as this |
|
1567 |
iterator; otherwise returns false. |
|
1568 |
||
1569 |
\sa operator!=() |
|
1570 |
*/ |
|
1571 |
||
1572 |
/*! \fn bool QHash::const_iterator::operator!=(const const_iterator &other) const |
|
1573 |
||
1574 |
Returns true if \a other points to a different item than this |
|
1575 |
iterator; otherwise returns false. |
|
1576 |
||
1577 |
\sa operator==() |
|
1578 |
*/ |
|
1579 |
||
1580 |
/*! |
|
1581 |
\fn QHash::const_iterator &QHash::const_iterator::operator++() |
|
1582 |
||
1583 |
The prefix ++ operator (\c{++i}) advances the iterator to the |
|
1584 |
next item in the hash and returns an iterator to the new current |
|
1585 |
item. |
|
1586 |
||
1587 |
Calling this function on QHash::end() leads to undefined results. |
|
1588 |
||
1589 |
\sa operator--() |
|
1590 |
*/ |
|
1591 |
||
1592 |
/*! \fn QHash::const_iterator QHash::const_iterator::operator++(int) |
|
1593 |
||
1594 |
\overload |
|
1595 |
||
1596 |
The postfix ++ operator (\c{i++}) advances the iterator to the |
|
1597 |
next item in the hash and returns an iterator to the previously |
|
1598 |
current item. |
|
1599 |
*/ |
|
1600 |
||
1601 |
/*! \fn QHash::const_iterator &QHash::const_iterator::operator--() |
|
1602 |
||
1603 |
The prefix -- operator (\c{--i}) makes the preceding item |
|
1604 |
current and returns an iterator pointing to the new current item. |
|
1605 |
||
1606 |
Calling this function on QHash::begin() leads to undefined |
|
1607 |
results. |
|
1608 |
||
1609 |
\sa operator++() |
|
1610 |
*/ |
|
1611 |
||
1612 |
/*! \fn QHash::const_iterator QHash::const_iterator::operator--(int) |
|
1613 |
||
1614 |
\overload |
|
1615 |
||
1616 |
The postfix -- operator (\c{i--}) makes the preceding item |
|
1617 |
current and returns an iterator pointing to the previously |
|
1618 |
current item. |
|
1619 |
*/ |
|
1620 |
||
1621 |
/*! \fn QHash::const_iterator QHash::const_iterator::operator+(int j) const |
|
1622 |
||
1623 |
Returns an iterator to the item at \a j positions forward from |
|
1624 |
this iterator. (If \a j is negative, the iterator goes backward.) |
|
1625 |
||
1626 |
This operation can be slow for large \a j values. |
|
1627 |
||
1628 |
\sa operator-() |
|
1629 |
*/ |
|
1630 |
||
1631 |
/*! \fn QHash::const_iterator QHash::const_iterator::operator-(int j) const |
|
1632 |
||
1633 |
Returns an iterator to the item at \a j positions backward from |
|
1634 |
this iterator. (If \a j is negative, the iterator goes forward.) |
|
1635 |
||
1636 |
This operation can be slow for large \a j values. |
|
1637 |
||
1638 |
\sa operator+() |
|
1639 |
*/ |
|
1640 |
||
1641 |
/*! \fn QHash::const_iterator &QHash::const_iterator::operator+=(int j) |
|
1642 |
||
1643 |
Advances the iterator by \a j items. (If \a j is negative, the |
|
1644 |
iterator goes backward.) |
|
1645 |
||
1646 |
This operation can be slow for large \a j values. |
|
1647 |
||
1648 |
\sa operator-=(), operator+() |
|
1649 |
*/ |
|
1650 |
||
1651 |
/*! \fn QHash::const_iterator &QHash::const_iterator::operator-=(int j) |
|
1652 |
||
1653 |
Makes the iterator go back by \a j items. (If \a j is negative, |
|
1654 |
the iterator goes forward.) |
|
1655 |
||
1656 |
This operation can be slow for large \a j values. |
|
1657 |
||
1658 |
\sa operator+=(), operator-() |
|
1659 |
*/ |
|
1660 |
||
1661 |
/*! \fn QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash) |
|
1662 |
\relates QHash |
|
1663 |
||
1664 |
Writes the hash \a hash to stream \a out. |
|
1665 |
||
1666 |
This function requires the key and value types to implement \c |
|
1667 |
operator<<(). |
|
1668 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1669 |
\sa {Serializing Qt Data Types} |
0 | 1670 |
*/ |
1671 |
||
1672 |
/*! \fn QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash) |
|
1673 |
\relates QHash |
|
1674 |
||
1675 |
Reads a hash from stream \a in into \a hash. |
|
1676 |
||
1677 |
This function requires the key and value types to implement \c |
|
1678 |
operator>>(). |
|
1679 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
18
diff
changeset
|
1680 |
\sa {Serializing Qt Data Types} |
0 | 1681 |
*/ |
1682 |
||
1683 |
/*! \class QMultiHash |
|
1684 |
\brief The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes. |
|
1685 |
||
1686 |
\ingroup tools |
|
1687 |
\ingroup shared |
|
1688 |
||
1689 |
\reentrant |
|
1690 |
||
1691 |
QMultiHash\<Key, T\> is one of Qt's generic \l{container classes}. |
|
1692 |
It inherits QHash and extends it with a few convenience functions |
|
1693 |
that make it more suitable than QHash for storing multi-valued |
|
1694 |
hashes. A multi-valued hash is a hash that allows multiple values |
|
1695 |
with the same key; QHash normally doesn't allow that, unless you |
|
1696 |
call QHash::insertMulti(). |
|
1697 |
||
1698 |
Because QMultiHash inherits QHash, all of QHash's functionality also |
|
1699 |
applies to QMultiHash. For example, you can use isEmpty() to test |
|
1700 |
whether the hash is empty, and you can traverse a QMultiHash using |
|
1701 |
QHash's iterator classes (for example, QHashIterator). But in |
|
1702 |
addition, it provides an insert() function that corresponds to |
|
1703 |
QHash::insertMulti(), and a replace() function that corresponds to |
|
1704 |
QHash::insert(). It also provides convenient operator+() and |
|
1705 |
operator+=(). |
|
1706 |
||
1707 |
Example: |
|
1708 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 24 |
|
1709 |
||
1710 |
Unlike QHash, QMultiHash provides no operator[]. Use value() or |
|
1711 |
replace() if you want to access the most recently inserted item |
|
1712 |
with a certain key. |
|
1713 |
||
1714 |
If you want to retrieve all the values for a single key, you can |
|
1715 |
use values(const Key &key), which returns a QList<T>: |
|
1716 |
||
1717 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 25 |
|
1718 |
||
1719 |
The items that share the same key are available from most |
|
1720 |
recently to least recently inserted. |
|
1721 |
||
1722 |
A more efficient approach is to call find() to get |
|
1723 |
the STL-style iterator for the first item with a key and iterate from |
|
1724 |
there: |
|
1725 |
||
1726 |
\snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 26 |
|
1727 |
||
1728 |
QMultiHash's key and value data types must be \l{assignable data |
|
1729 |
types}. You cannot, for example, store a QWidget as a value; |
|
1730 |
instead, store a QWidget *. In addition, QMultiHash's key type |
|
1731 |
must provide operator==(), and there must also be a global |
|
1732 |
qHash() function that returns a hash value for an argument of the |
|
1733 |
key's type. See the QHash documentation for details. |
|
1734 |
||
1735 |
\sa QHash, QHashIterator, QMutableHashIterator, QMultiMap |
|
1736 |
*/ |
|
1737 |
||
1738 |
/*! \fn QMultiHash::QMultiHash() |
|
1739 |
||
1740 |
Constructs an empty hash. |
|
1741 |
*/ |
|
1742 |
||
1743 |
/*! \fn QMultiHash::QMultiHash(const QHash<Key, T> &other) |
|
1744 |
||
1745 |
Constructs a copy of \a other (which can be a QHash or a |
|
1746 |
QMultiHash). |
|
1747 |
||
1748 |
\sa operator=() |
|
1749 |
*/ |
|
1750 |
||
1751 |
/*! \fn QMultiHash::iterator QMultiHash::replace(const Key &key, const T &value) |
|
1752 |
||
1753 |
Inserts a new item with the \a key and a value of \a value. |
|
1754 |
||
1755 |
If there is already an item with the \a key, that item's value |
|
1756 |
is replaced with \a value. |
|
1757 |
||
1758 |
If there are multiple items with the \a key, the most |
|
1759 |
recently inserted item's value is replaced with \a value. |
|
1760 |
||
1761 |
\sa insert() |
|
1762 |
*/ |
|
1763 |
||
1764 |
/*! \fn QMultiHash::iterator QMultiHash::insert(const Key &key, const T &value) |
|
1765 |
||
1766 |
Inserts a new item with the \a key and a value of \a value. |
|
1767 |
||
1768 |
If there is already an item with the same key in the hash, this |
|
1769 |
function will simply create a new one. (This behavior is |
|
1770 |
different from replace(), which overwrites the value of an |
|
1771 |
existing item.) |
|
1772 |
||
1773 |
\sa replace() |
|
1774 |
*/ |
|
1775 |
||
1776 |
/*! \fn QMultiHash &QMultiHash::operator+=(const QMultiHash &other) |
|
1777 |
||
1778 |
Inserts all the items in the \a other hash into this hash |
|
1779 |
and returns a reference to this hash. |
|
1780 |
||
1781 |
\sa insert() |
|
1782 |
*/ |
|
1783 |
||
1784 |
/*! \fn QMultiHash QMultiHash::operator+(const QMultiHash &other) const |
|
1785 |
||
1786 |
Returns a hash that contains all the items in this hash in |
|
1787 |
addition to all the items in \a other. If a key is common to both |
|
1788 |
hashes, the resulting hash will contain the key multiple times. |
|
1789 |
||
1790 |
\sa operator+=() |
|
1791 |
*/ |
|
1792 |
||
1793 |
/*! |
|
1794 |
\fn bool QMultiHash::contains(const Key &key, const T &value) const |
|
1795 |
\since 4.3 |
|
1796 |
||
1797 |
Returns true if the hash contains an item with the \a key and |
|
1798 |
\a value; otherwise returns false. |
|
1799 |
||
1800 |
\sa QHash::contains() |
|
1801 |
*/ |
|
1802 |
||
1803 |
/*! |
|
1804 |
\fn bool QMultiHash::contains(const Key &key) const |
|
1805 |
\overload |
|
1806 |
\sa QHash::contains() |
|
1807 |
*/ |
|
1808 |
||
1809 |
/*! |
|
1810 |
\fn int QMultiHash::remove(const Key &key, const T &value) |
|
1811 |
\since 4.3 |
|
1812 |
||
1813 |
Removes all the items that have the \a key and the value \a |
|
1814 |
value from the hash. Returns the number of items removed. |
|
1815 |
||
1816 |
\sa QHash::remove() |
|
1817 |
*/ |
|
1818 |
||
1819 |
/*! |
|
1820 |
\fn int QMultiHash::remove(const Key &key) |
|
1821 |
\overload |
|
1822 |
\sa QHash::remove() |
|
1823 |
*/ |
|
1824 |
||
1825 |
/*! |
|
1826 |
\fn int QMultiHash::count(const Key &key, const T &value) const |
|
1827 |
\since 4.3 |
|
1828 |
||
1829 |
Returns the number of items with the \a key and \a value. |
|
1830 |
||
1831 |
\sa QHash::count() |
|
1832 |
*/ |
|
1833 |
||
1834 |
/*! |
|
1835 |
\fn int QMultiHash::count(const Key &key) const |
|
1836 |
\overload |
|
1837 |
\sa QHash::count() |
|
1838 |
*/ |
|
1839 |
||
1840 |
/*! |
|
1841 |
\fn int QMultiHash::count() const |
|
1842 |
\overload |
|
1843 |
\sa QHash::count() |
|
1844 |
*/ |
|
1845 |
||
1846 |
/*! |
|
1847 |
\fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key, const T &value) |
|
1848 |
\since 4.3 |
|
1849 |
||
1850 |
Returns an iterator pointing to the item with the \a key and \a value. |
|
1851 |
If the hash contains no such item, the function returns end(). |
|
1852 |
||
1853 |
If the hash contains multiple items with the \a key and \a value, the |
|
1854 |
iterator returned points to the most recently inserted item. |
|
1855 |
||
1856 |
\sa QHash::find() |
|
1857 |
*/ |
|
1858 |
||
1859 |
/*! |
|
1860 |
\fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key) |
|
1861 |
\overload |
|
1862 |
\sa QHash::find() |
|
1863 |
*/ |
|
1864 |
||
1865 |
/*! |
|
1866 |
\fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key, const T &value) const |
|
1867 |
\since 4.3 |
|
1868 |
\overload |
|
1869 |
*/ |
|
1870 |
||
1871 |
/*! |
|
1872 |
\fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key) const |
|
1873 |
\overload |
|
1874 |
\sa QHash::find() |
|
1875 |
*/ |
|
1876 |
||
1877 |
/*! |
|
1878 |
\fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key, const T &value) const |
|
1879 |
\since 4.3 |
|
1880 |
||
1881 |
Returns an iterator pointing to the item with the \a key and the |
|
1882 |
\a value in the hash. |
|
1883 |
||
1884 |
If the hash contains no such item, the function returns |
|
1885 |
constEnd(). |
|
1886 |
||
1887 |
\sa QHash::constFind() |
|
1888 |
*/ |
|
1889 |
||
1890 |
/*! |
|
1891 |
\fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key) const |
|
1892 |
\overload |
|
1893 |
\sa QHash::constFind() |
|
1894 |
*/ |
|
1895 |
||
1896 |
QT_END_NAMESPACE |