1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtCore module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
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 | uint g;
|
---|
68 |
|
---|
69 | while (n--) {
|
---|
70 | h = (h << 4) + *p++;
|
---|
71 | if ((g = (h & 0xf0000000)) != 0)
|
---|
72 | h ^= g >> 23;
|
---|
73 | h &= ~g;
|
---|
74 | }
|
---|
75 | return h;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static uint hash(const QChar *p, int n)
|
---|
79 | {
|
---|
80 | uint h = 0;
|
---|
81 | uint g;
|
---|
82 |
|
---|
83 | while (n--) {
|
---|
84 | h = (h << 4) + (*p++).unicode();
|
---|
85 | if ((g = (h & 0xf0000000)) != 0)
|
---|
86 | h ^= g >> 23;
|
---|
87 | h &= ~g;
|
---|
88 | }
|
---|
89 | return h;
|
---|
90 | }
|
---|
91 |
|
---|
92 | uint qHash(const QByteArray &key)
|
---|
93 | {
|
---|
94 | return hash(reinterpret_cast<const uchar *>(key.data()), key.size());
|
---|
95 | }
|
---|
96 |
|
---|
97 | uint qHash(const QString &key)
|
---|
98 | {
|
---|
99 | return hash(key.unicode(), key.size());
|
---|
100 | }
|
---|
101 |
|
---|
102 | uint qHash(const QStringRef &key)
|
---|
103 | {
|
---|
104 | return hash(key.unicode(), key.size());
|
---|
105 | }
|
---|
106 |
|
---|
107 | uint qHash(const QBitArray &bitArray)
|
---|
108 | {
|
---|
109 | int m = bitArray.d.size() - 1;
|
---|
110 | uint result = hash(reinterpret_cast<const uchar *>(bitArray.d.data()), qMax(0, m));
|
---|
111 |
|
---|
112 | // deal with the last 0 to 7 bits manually, because we can't trust that
|
---|
113 | // the padding is initialized to 0 in bitArray.d
|
---|
114 | int n = bitArray.size();
|
---|
115 | if (n & 0x7)
|
---|
116 | result = ((result << 4) + bitArray.d.at(m)) & ((1 << n) - 1);
|
---|
117 | return result;
|
---|
118 | }
|
---|
119 |
|
---|
120 | /*
|
---|
121 | The prime_deltas array is a table of selected prime values, even
|
---|
122 | though it doesn't look like one. The primes we are using are 1,
|
---|
123 | 2, 5, 11, 17, 37, 67, 131, 257, ..., i.e. primes in the immediate
|
---|
124 | surrounding of a power of two.
|
---|
125 |
|
---|
126 | The primeForNumBits() function returns the prime associated to a
|
---|
127 | power of two. For example, primeForNumBits(8) returns 257.
|
---|
128 | */
|
---|
129 |
|
---|
130 | static const uchar prime_deltas[] = {
|
---|
131 | 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3,
|
---|
132 | 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0
|
---|
133 | };
|
---|
134 |
|
---|
135 | static inline int primeForNumBits(int numBits)
|
---|
136 | {
|
---|
137 | return (1 << numBits) + prime_deltas[numBits];
|
---|
138 | }
|
---|
139 |
|
---|
140 | /*
|
---|
141 | Returns the smallest integer n such that
|
---|
142 | primeForNumBits(n) >= hint.
|
---|
143 | */
|
---|
144 | static int countBits(int hint)
|
---|
145 | {
|
---|
146 | int numBits = 0;
|
---|
147 | int bits = hint;
|
---|
148 |
|
---|
149 | while (bits > 1) {
|
---|
150 | bits >>= 1;
|
---|
151 | numBits++;
|
---|
152 | }
|
---|
153 |
|
---|
154 | if (numBits >= (int)sizeof(prime_deltas)) {
|
---|
155 | numBits = sizeof(prime_deltas) - 1;
|
---|
156 | } else if (primeForNumBits(numBits) < hint) {
|
---|
157 | ++numBits;
|
---|
158 | }
|
---|
159 | return numBits;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /*
|
---|
163 | A QHash has initially around pow(2, MinNumBits) buckets. For
|
---|
164 | example, if MinNumBits is 4, it has 17 buckets.
|
---|
165 | */
|
---|
166 | const int MinNumBits = 4;
|
---|
167 |
|
---|
168 | QHashData QHashData::shared_null = {
|
---|
169 | 0, 0, Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, MinNumBits, 0, 0, true
|
---|
170 | };
|
---|
171 |
|
---|
172 | void *QHashData::allocateNode()
|
---|
173 | {
|
---|
174 | return qMalloc(nodeSize);
|
---|
175 | }
|
---|
176 |
|
---|
177 | void QHashData::freeNode(void *node)
|
---|
178 | {
|
---|
179 | qFree(node);
|
---|
180 | }
|
---|
181 |
|
---|
182 | QHashData *QHashData::detach_helper(void (*node_duplicate)(Node *, void *), int nodeSize)
|
---|
183 | {
|
---|
184 | union {
|
---|
185 | QHashData *d;
|
---|
186 | Node *e;
|
---|
187 | };
|
---|
188 | d = new QHashData;
|
---|
189 | d->fakeNext = 0;
|
---|
190 | d->buckets = 0;
|
---|
191 | d->ref = 1;
|
---|
192 | d->size = size;
|
---|
193 | d->nodeSize = nodeSize;
|
---|
194 | d->userNumBits = userNumBits;
|
---|
195 | d->numBits = numBits;
|
---|
196 | d->numBuckets = numBuckets;
|
---|
197 | d->sharable = true;
|
---|
198 |
|
---|
199 | if (numBuckets) {
|
---|
200 | d->buckets = new Node *[numBuckets];
|
---|
201 | Node *this_e = reinterpret_cast<Node *>(this);
|
---|
202 | for (int i = 0; i < numBuckets; ++i) {
|
---|
203 | Node **nextNode = &d->buckets[i];
|
---|
204 | Node *oldNode = buckets[i];
|
---|
205 | while (oldNode != this_e) {
|
---|
206 | Node *dup = static_cast<Node *>(allocateNode());
|
---|
207 | node_duplicate(oldNode, dup);
|
---|
208 | dup->h = oldNode->h;
|
---|
209 | *nextNode = dup;
|
---|
210 | nextNode = &dup->next;
|
---|
211 | oldNode = oldNode->next;
|
---|
212 | }
|
---|
213 | *nextNode = e;
|
---|
214 | }
|
---|
215 | }
|
---|
216 | return d;
|
---|
217 | }
|
---|
218 |
|
---|
219 | QHashData::Node *QHashData::nextNode(Node *node)
|
---|
220 | {
|
---|
221 | union {
|
---|
222 | Node *next;
|
---|
223 | Node *e;
|
---|
224 | QHashData *d;
|
---|
225 | };
|
---|
226 | next = node->next;
|
---|
227 | Q_ASSERT_X(next, "QHash", "Iterating beyond end()");
|
---|
228 | if (next->next)
|
---|
229 | return next;
|
---|
230 |
|
---|
231 | int start = (node->h % d->numBuckets) + 1;
|
---|
232 | Node **bucket = d->buckets + start;
|
---|
233 | int n = d->numBuckets - start;
|
---|
234 | while (n--) {
|
---|
235 | if (*bucket != e)
|
---|
236 | return *bucket;
|
---|
237 | ++bucket;
|
---|
238 | }
|
---|
239 | return e;
|
---|
240 | }
|
---|
241 |
|
---|
242 | QHashData::Node *QHashData::previousNode(Node *node)
|
---|
243 | {
|
---|
244 | union {
|
---|
245 | Node *e;
|
---|
246 | QHashData *d;
|
---|
247 | };
|
---|
248 |
|
---|
249 | e = node;
|
---|
250 | while (e->next)
|
---|
251 | e = e->next;
|
---|
252 |
|
---|
253 | int start;
|
---|
254 | if (node == e)
|
---|
255 | start = d->numBuckets - 1;
|
---|
256 | else
|
---|
257 | start = node->h % d->numBuckets;
|
---|
258 |
|
---|
259 | Node *sentinel = node;
|
---|
260 | Node **bucket = d->buckets + start;
|
---|
261 | while (start >= 0) {
|
---|
262 | if (*bucket != sentinel) {
|
---|
263 | Node *prev = *bucket;
|
---|
264 | while (prev->next != sentinel)
|
---|
265 | prev = prev->next;
|
---|
266 | return prev;
|
---|
267 | }
|
---|
268 |
|
---|
269 | sentinel = e;
|
---|
270 | --bucket;
|
---|
271 | --start;
|
---|
272 | }
|
---|
273 | Q_ASSERT_X(start >= 0, "QHash", "Iterating backward beyond begin()");
|
---|
274 | return e;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /*
|
---|
278 | If hint is negative, -hint gives the approximate number of
|
---|
279 | buckets that should be used for the hash table. If hint is
|
---|
280 | nonnegative, (1 << hint) gives the approximate number
|
---|
281 | of buckets that should be used.
|
---|
282 | */
|
---|
283 | void QHashData::rehash(int hint)
|
---|
284 | {
|
---|
285 | if (hint < 0) {
|
---|
286 | hint = countBits(-hint);
|
---|
287 | if (hint < MinNumBits)
|
---|
288 | hint = MinNumBits;
|
---|
289 | userNumBits = hint;
|
---|
290 | while (primeForNumBits(hint) < (size >> 1))
|
---|
291 | ++hint;
|
---|
292 | } else if (hint < MinNumBits) {
|
---|
293 | hint = MinNumBits;
|
---|
294 | }
|
---|
295 |
|
---|
296 | if (numBits != hint) {
|
---|
297 | Node *e = reinterpret_cast<Node *>(this);
|
---|
298 | Node **oldBuckets = buckets;
|
---|
299 | int oldNumBuckets = numBuckets;
|
---|
300 |
|
---|
301 | numBits = hint;
|
---|
302 | numBuckets = primeForNumBits(hint);
|
---|
303 | buckets = new Node *[numBuckets];
|
---|
304 | for (int i = 0; i < numBuckets; ++i)
|
---|
305 | buckets[i] = e;
|
---|
306 |
|
---|
307 | for (int i = 0; i < oldNumBuckets; ++i) {
|
---|
308 | Node *firstNode = oldBuckets[i];
|
---|
309 | while (firstNode != e) {
|
---|
310 | uint h = firstNode->h;
|
---|
311 | Node *lastNode = firstNode;
|
---|
312 | while (lastNode->next != e && lastNode->next->h == h)
|
---|
313 | lastNode = lastNode->next;
|
---|
314 |
|
---|
315 | Node *afterLastNode = lastNode->next;
|
---|
316 | Node **beforeFirstNode = &buckets[h % numBuckets];
|
---|
317 | while (*beforeFirstNode != e)
|
---|
318 | beforeFirstNode = &(*beforeFirstNode)->next;
|
---|
319 | lastNode->next = *beforeFirstNode;
|
---|
320 | *beforeFirstNode = firstNode;
|
---|
321 | firstNode = afterLastNode;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | delete [] oldBuckets;
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | void QHashData::destroyAndFree()
|
---|
329 | {
|
---|
330 | delete [] buckets;
|
---|
331 | delete this;
|
---|
332 | }
|
---|
333 |
|
---|
334 | #ifdef QT_QHASH_DEBUG
|
---|
335 |
|
---|
336 | void QHashData::dump()
|
---|
337 | {
|
---|
338 | qDebug("Hash data (ref = %d, size = %d, nodeSize = %d, userNumBits = %d, numBits = %d, numBuckets = %d)",
|
---|
339 | int(ref), size, nodeSize, userNumBits, numBits,
|
---|
340 | numBuckets);
|
---|
341 | qDebug(" %p (fakeNode = %p)", this, fakeNext);
|
---|
342 | for (int i = 0; i < numBuckets; ++i) {
|
---|
343 | QString line;
|
---|
344 | Node *n = buckets[i];
|
---|
345 | if (n != reinterpret_cast<Node *>(this)) {
|
---|
346 | line.sprintf("%d:", i);
|
---|
347 | while (n != reinterpret_cast<Node *>(this)) {
|
---|
348 | line += QString().sprintf(" -> [%p]", n);
|
---|
349 | if (!n) {
|
---|
350 | line += " (CORRUPT)";
|
---|
351 | break;
|
---|
352 | }
|
---|
353 | n = n->next;
|
---|
354 | }
|
---|
355 | qDebug(qPrintable(line));
|
---|
356 | }
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | void QHashData::checkSanity()
|
---|
361 | {
|
---|
362 | if (fakeNext)
|
---|
363 | qFatal("Fake next isn't 0");
|
---|
364 |
|
---|
365 | for (int i = 0; i < numBuckets; ++i) {
|
---|
366 | Node *n = buckets[i];
|
---|
367 | Node *p = n;
|
---|
368 | if (!n)
|
---|
369 | qFatal("%d: Bucket entry is 0", i);
|
---|
370 | if (n != reinterpret_cast<Node *>(this)) {
|
---|
371 | while (n != reinterpret_cast<Node *>(this)) {
|
---|
372 | if (!n->next)
|
---|
373 | qFatal("%d: Next of %p is 0, should be %p", i, n, this);
|
---|
374 | n = n->next;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | }
|
---|
379 | #endif
|
---|
380 |
|
---|
381 | /*!
|
---|
382 | \class QHash
|
---|
383 | \brief The QHash class is a template class that provides a hash-table-based dictionary.
|
---|
384 |
|
---|
385 | \ingroup tools
|
---|
386 | \ingroup shared
|
---|
387 | \mainclass
|
---|
388 | \reentrant
|
---|
389 |
|
---|
390 | QHash\<Key, T\> is one of Qt's generic \l{container classes}. It
|
---|
391 | stores (key, value) pairs and provides very fast lookup of the
|
---|
392 | value associated with a key.
|
---|
393 |
|
---|
394 | QHash provides very similar functionality to QMap. The
|
---|
395 | differences are:
|
---|
396 |
|
---|
397 | \list
|
---|
398 | \i QHash provides faster lookups than QMap. (See \l{Algorithmic
|
---|
399 | Complexity} for details.)
|
---|
400 | \i When iterating over a QMap, the items are always sorted by
|
---|
401 | key. With QHash, the items are arbitrarily ordered.
|
---|
402 | \i The key type of a QMap must provide operator<(). The key
|
---|
403 | type of a QHash must provide operator==() and a global
|
---|
404 | \l{qHash()}{qHash}(Key) function.
|
---|
405 | \endlist
|
---|
406 |
|
---|
407 | Here's an example QHash with QString keys and \c int values:
|
---|
408 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 0
|
---|
409 |
|
---|
410 | To insert a (key, value) pair into the hash, you can use operator[]():
|
---|
411 |
|
---|
412 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 1
|
---|
413 |
|
---|
414 | This inserts the following three (key, value) pairs into the
|
---|
415 | QHash: ("one", 1), ("three", 3), and ("seven", 7). Another way to
|
---|
416 | insert items into the hash is to use insert():
|
---|
417 |
|
---|
418 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 2
|
---|
419 |
|
---|
420 | To look up a value, use operator[]() or value():
|
---|
421 |
|
---|
422 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 3
|
---|
423 |
|
---|
424 | If there is no item with the specified key in the hash, these
|
---|
425 | functions return a \l{default-constructed value}.
|
---|
426 |
|
---|
427 | If you want to check whether the hash contains a particular key,
|
---|
428 | use contains():
|
---|
429 |
|
---|
430 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 4
|
---|
431 |
|
---|
432 | There is also a value() overload that uses its second argument as
|
---|
433 | a default value if there is no item with the specified key:
|
---|
434 |
|
---|
435 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 5
|
---|
436 |
|
---|
437 | In general, we recommend that you use contains() and value()
|
---|
438 | rather than operator[]() for looking up a key in a hash. The
|
---|
439 | reason is that operator[]() silently inserts an item into the
|
---|
440 | hash if no item exists with the same key (unless the hash is
|
---|
441 | const). For example, the following code snippet will create 1000
|
---|
442 | items in memory:
|
---|
443 |
|
---|
444 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 6
|
---|
445 |
|
---|
446 | To avoid this problem, replace \c hash[i] with \c hash.value(i)
|
---|
447 | in the code above.
|
---|
448 |
|
---|
449 | If you want to navigate through all the (key, value) pairs stored
|
---|
450 | in a QHash, you can use an iterator. QHash provides both
|
---|
451 | \l{Java-style iterators} (QHashIterator and QMutableHashIterator)
|
---|
452 | and \l{STL-style iterators} (QHash::const_iterator and
|
---|
453 | QHash::iterator). Here's how to iterate over a QHash<QString,
|
---|
454 | int> using a Java-style iterator:
|
---|
455 |
|
---|
456 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 7
|
---|
457 |
|
---|
458 | Here's the same code, but using an STL-style iterator:
|
---|
459 |
|
---|
460 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 8
|
---|
461 |
|
---|
462 | QHash is unordered, so an iterator's sequence cannot be assumed
|
---|
463 | to be predictable. If ordering by key is required, use a QMap.
|
---|
464 |
|
---|
465 | Normally, a QHash allows only one value per key. If you call
|
---|
466 | insert() with a key that already exists in the QHash, the
|
---|
467 | previous value is erased. For example:
|
---|
468 |
|
---|
469 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 9
|
---|
470 |
|
---|
471 | However, you can store multiple values per key by using
|
---|
472 | insertMulti() instead of insert() (or using the convenience
|
---|
473 | subclass QMultiHash). If you want to retrieve all
|
---|
474 | the values for a single key, you can use values(const Key &key),
|
---|
475 | which returns a QList<T>:
|
---|
476 |
|
---|
477 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 10
|
---|
478 |
|
---|
479 | The items that share the same key are available from most
|
---|
480 | recently to least recently inserted. A more efficient approach is
|
---|
481 | to call find() to get the iterator for the first item with a key
|
---|
482 | and iterate from there:
|
---|
483 |
|
---|
484 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 11
|
---|
485 |
|
---|
486 | If you only need to extract the values from a hash (not the keys),
|
---|
487 | you can also use \l{foreach}:
|
---|
488 |
|
---|
489 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 12
|
---|
490 |
|
---|
491 | Items can be removed from the hash in several ways. One way is to
|
---|
492 | call remove(); this will remove any item with the given key.
|
---|
493 | Another way is to use QMutableHashIterator::remove(). In addition,
|
---|
494 | you can clear the entire hash using clear().
|
---|
495 |
|
---|
496 | QHash's key and value data types must be \l{assignable data
|
---|
497 | types}. You cannot, for example, store a QWidget as a value;
|
---|
498 | instead, store a QWidget *. In addition, QHash's key type must
|
---|
499 | provide operator==(), and there must also be a global qHash()
|
---|
500 | function that returns a hash value for an argument of the key's
|
---|
501 | type.
|
---|
502 |
|
---|
503 | Here's a list of the C++ and Qt types that can serve as keys in a
|
---|
504 | QHash: any integer type (char, unsigned long, etc.), any pointer
|
---|
505 | type, QChar, QString, and QByteArray. For all of these, the \c
|
---|
506 | <QHash> header defines a qHash() function that computes an
|
---|
507 | adequate hash value. If you want to use other types as the key,
|
---|
508 | make sure that you provide operator==() and a qHash()
|
---|
509 | implementation.
|
---|
510 |
|
---|
511 | Example:
|
---|
512 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 13
|
---|
513 |
|
---|
514 | The qHash() function computes a numeric value based on a key. It
|
---|
515 | can use any algorithm imaginable, as long as it always returns
|
---|
516 | the same value if given the same argument. In other words, if
|
---|
517 | \c{e1 == e2}, then \c{qHash(e1) == qHash(e2)} must hold as well.
|
---|
518 | However, to obtain good performance, the qHash() function should
|
---|
519 | attempt to return different hash values for different keys to the
|
---|
520 | largest extent possible.
|
---|
521 |
|
---|
522 | In the example above, we've relied on Qt's global qHash(const
|
---|
523 | QString &) to give us a hash value for the employee's name, and
|
---|
524 | XOR'ed this with the day they were born to help produce unique
|
---|
525 | hashes for people with the same name.
|
---|
526 |
|
---|
527 | Internally, QHash uses a hash table to perform lookups. Unlike Qt
|
---|
528 | 3's \c QDict class, which needed to be initialized with a prime
|
---|
529 | number, QHash's hash table automatically grows and shrinks to
|
---|
530 | provide fast lookups without wasting too much memory. You can
|
---|
531 | still control the size of the hash table by calling reserve() if
|
---|
532 | you already know approximately how many items the QHash will
|
---|
533 | contain, but this isn't necessary to obtain good performance. You
|
---|
534 | can also call capacity() to retrieve the hash table's size.
|
---|
535 |
|
---|
536 | \sa QHashIterator, QMutableHashIterator, QMap, QSet
|
---|
537 | */
|
---|
538 |
|
---|
539 | /*! \fn QHash::QHash()
|
---|
540 |
|
---|
541 | Constructs an empty hash.
|
---|
542 |
|
---|
543 | \sa clear()
|
---|
544 | */
|
---|
545 |
|
---|
546 | /*! \fn QHash::QHash(const QHash<Key, T> &other)
|
---|
547 |
|
---|
548 | Constructs a copy of \a other.
|
---|
549 |
|
---|
550 | This operation occurs in \l{constant time}, because QHash is
|
---|
551 | \l{implicitly shared}. This makes returning a QHash from a
|
---|
552 | function very fast. If a shared instance is modified, it will be
|
---|
553 | copied (copy-on-write), and this takes \l{linear time}.
|
---|
554 |
|
---|
555 | \sa operator=()
|
---|
556 | */
|
---|
557 |
|
---|
558 | /*! \fn QHash::~QHash()
|
---|
559 |
|
---|
560 | Destroys the hash. References to the values in the hash and all
|
---|
561 | iterators of this hash become invalid.
|
---|
562 | */
|
---|
563 |
|
---|
564 | /*! \fn QHash<Key, T> &QHash::operator=(const QHash<Key, T> &other)
|
---|
565 |
|
---|
566 | Assigns \a other to this hash and returns a reference to this hash.
|
---|
567 | */
|
---|
568 |
|
---|
569 | /*! \fn bool QHash::operator==(const QHash<Key, T> &other) const
|
---|
570 |
|
---|
571 | Returns true if \a other is equal to this hash; otherwise returns
|
---|
572 | false.
|
---|
573 |
|
---|
574 | Two hashes are considered equal if they contain the same (key,
|
---|
575 | value) pairs.
|
---|
576 |
|
---|
577 | This function requires the value type to implement \c operator==().
|
---|
578 |
|
---|
579 | \sa operator!=()
|
---|
580 | */
|
---|
581 |
|
---|
582 | /*! \fn bool QHash::operator!=(const QHash<Key, T> &other) const
|
---|
583 |
|
---|
584 | Returns true if \a other is not equal to this hash; otherwise
|
---|
585 | returns false.
|
---|
586 |
|
---|
587 | Two hashes are considered equal if they contain the same (key,
|
---|
588 | value) pairs.
|
---|
589 |
|
---|
590 | This function requires the value type to implement \c operator==().
|
---|
591 |
|
---|
592 | \sa operator==()
|
---|
593 | */
|
---|
594 |
|
---|
595 | /*! \fn int QHash::size() const
|
---|
596 |
|
---|
597 | Returns the number of items in the hash.
|
---|
598 |
|
---|
599 | \sa isEmpty(), count()
|
---|
600 | */
|
---|
601 |
|
---|
602 | /*! \fn bool QHash::isEmpty() const
|
---|
603 |
|
---|
604 | Returns true if the hash contains no items; otherwise returns
|
---|
605 | false.
|
---|
606 |
|
---|
607 | \sa size()
|
---|
608 | */
|
---|
609 |
|
---|
610 | /*! \fn int QHash::capacity() const
|
---|
611 |
|
---|
612 | Returns the number of buckets in the QHash's internal hash table.
|
---|
613 |
|
---|
614 | The sole purpose of this function is to provide a means of fine
|
---|
615 | tuning QHash's memory usage. In general, you will rarely ever
|
---|
616 | need to call this function. If you want to know how many items are
|
---|
617 | in the hash, call size().
|
---|
618 |
|
---|
619 | \sa reserve(), squeeze()
|
---|
620 | */
|
---|
621 |
|
---|
622 | /*! \fn void QHash::reserve(int size)
|
---|
623 |
|
---|
624 | Ensures that the QHash's internal hash table consists of at least
|
---|
625 | \a size buckets.
|
---|
626 |
|
---|
627 | This function is useful for code that needs to build a huge hash
|
---|
628 | and wants to avoid repeated reallocation. For example:
|
---|
629 |
|
---|
630 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 14
|
---|
631 |
|
---|
632 | Ideally, \a size should be slightly more than the maximum number
|
---|
633 | of items expected in the hash. \a size doesn't have to be prime,
|
---|
634 | because QHash will use a prime number internally anyway. If \a size
|
---|
635 | is an underestimate, the worst that will happen is that the QHash
|
---|
636 | will be a bit slower.
|
---|
637 |
|
---|
638 | In general, you will rarely ever need to call this function.
|
---|
639 | QHash's internal hash table automatically shrinks or grows to
|
---|
640 | provide good performance without wasting too much memory.
|
---|
641 |
|
---|
642 | \sa squeeze(), capacity()
|
---|
643 | */
|
---|
644 |
|
---|
645 | /*! \fn void QHash::squeeze()
|
---|
646 |
|
---|
647 | Reduces the size of the QHash's internal hash table to save
|
---|
648 | memory.
|
---|
649 |
|
---|
650 | The sole purpose of this function is to provide a means of fine
|
---|
651 | tuning QHash's memory usage. In general, you will rarely ever
|
---|
652 | need to call this function.
|
---|
653 |
|
---|
654 | \sa reserve(), capacity()
|
---|
655 | */
|
---|
656 |
|
---|
657 | /*! \fn void QHash::detach()
|
---|
658 |
|
---|
659 | \internal
|
---|
660 |
|
---|
661 | Detaches this hash from any other hashes with which it may share
|
---|
662 | data.
|
---|
663 |
|
---|
664 | \sa isDetached()
|
---|
665 | */
|
---|
666 |
|
---|
667 | /*! \fn bool QHash::isDetached() const
|
---|
668 |
|
---|
669 | \internal
|
---|
670 |
|
---|
671 | Returns true if the hash's internal data isn't shared with any
|
---|
672 | other hash object; otherwise returns false.
|
---|
673 |
|
---|
674 | \sa detach()
|
---|
675 | */
|
---|
676 |
|
---|
677 | /*! \fn void QHash::setSharable(bool sharable)
|
---|
678 |
|
---|
679 | \internal
|
---|
680 | */
|
---|
681 |
|
---|
682 | /*! \fn void QHash::clear()
|
---|
683 |
|
---|
684 | Removes all items from the hash.
|
---|
685 |
|
---|
686 | \sa remove()
|
---|
687 | */
|
---|
688 |
|
---|
689 | /*! \fn int QHash::remove(const Key &key)
|
---|
690 |
|
---|
691 | Removes all the items that have the \a key from the hash.
|
---|
692 | Returns the number of items removed which is usually 1 but will
|
---|
693 | be 0 if the key isn't in the hash, or greater than 1 if
|
---|
694 | insertMulti() has been used with the \a key.
|
---|
695 |
|
---|
696 | \sa clear(), take(), QMultiHash::remove()
|
---|
697 | */
|
---|
698 |
|
---|
699 | /*! \fn T QHash::take(const Key &key)
|
---|
700 |
|
---|
701 | Removes the item with the \a key from the hash and returns
|
---|
702 | the value associated with it.
|
---|
703 |
|
---|
704 | If the item does not exist in the hash, the function simply
|
---|
705 | returns a \l{default-constructed value}. If there are multiple
|
---|
706 | items for \a key in the hash, only the most recently inserted one
|
---|
707 | is removed.
|
---|
708 |
|
---|
709 | If you don't use the return value, remove() is more efficient.
|
---|
710 |
|
---|
711 | \sa remove()
|
---|
712 | */
|
---|
713 |
|
---|
714 | /*! \fn bool QHash::contains(const Key &key) const
|
---|
715 |
|
---|
716 | Returns true if the hash contains an item with the \a key;
|
---|
717 | otherwise returns false.
|
---|
718 |
|
---|
719 | \sa count(), QMultiHash::contains()
|
---|
720 | */
|
---|
721 |
|
---|
722 | /*! \fn const T QHash::value(const Key &key) const
|
---|
723 |
|
---|
724 | Returns the value associated with the \a key.
|
---|
725 |
|
---|
726 | If the hash contains no item with the \a key, the function
|
---|
727 | returns a \l{default-constructed value}. If there are multiple
|
---|
728 | items for the \a key in the hash, the value of the most recently
|
---|
729 | inserted one is returned.
|
---|
730 |
|
---|
731 | \sa key(), values(), contains(), operator[]()
|
---|
732 | */
|
---|
733 |
|
---|
734 | /*! \fn const T QHash::value(const Key &key, const T &defaultValue) const
|
---|
735 |
|
---|
736 | \overload
|
---|
737 |
|
---|
738 | If the hash contains no item with the given \a key, the function returns
|
---|
739 | \a defaultValue.
|
---|
740 | */
|
---|
741 |
|
---|
742 | /*! \fn T &QHash::operator[](const Key &key)
|
---|
743 |
|
---|
744 | Returns the value associated with the \a key as a modifiable
|
---|
745 | reference.
|
---|
746 |
|
---|
747 | If the hash contains no item with the \a key, the function inserts
|
---|
748 | a \l{default-constructed value} into the hash with the \a key, and
|
---|
749 | returns a reference to it. If the hash contains multiple items
|
---|
750 | with the \a key, this function returns a reference to the most
|
---|
751 | recently inserted value.
|
---|
752 |
|
---|
753 | \sa insert(), value()
|
---|
754 | */
|
---|
755 |
|
---|
756 | /*! \fn const T QHash::operator[](const Key &key) const
|
---|
757 |
|
---|
758 | \overload
|
---|
759 |
|
---|
760 | Same as value().
|
---|
761 | */
|
---|
762 |
|
---|
763 | /*! \fn QList<Key> QHash::uniqueKeys() const
|
---|
764 | \since 4.2
|
---|
765 |
|
---|
766 | Returns a list containing all the keys in the map. Keys that occur multiple
|
---|
767 | times in the map (because items were inserted with insertMulti(), or
|
---|
768 | unite() was used) occur only once in the returned list.
|
---|
769 |
|
---|
770 | \sa keys(), values()
|
---|
771 | */
|
---|
772 |
|
---|
773 | /*! \fn QList<Key> QHash::keys() const
|
---|
774 |
|
---|
775 | Returns a list containing all the keys in the hash, in an
|
---|
776 | arbitrary order. Keys that occur multiple times in the hash
|
---|
777 | (because items were inserted with insertMulti(), or unite() was
|
---|
778 | used) also occur multiple times in the list.
|
---|
779 |
|
---|
780 | To obtain a list of unique keys, where each key from the map only
|
---|
781 | occurs once, use uniqueKeys().
|
---|
782 |
|
---|
783 | The order is guaranteed to be the same as that used by values().
|
---|
784 |
|
---|
785 | \sa uniqueKeys(), values(), key()
|
---|
786 | */
|
---|
787 |
|
---|
788 | /*! \fn QList<Key> QHash::keys(const T &value) const
|
---|
789 |
|
---|
790 | \overload
|
---|
791 |
|
---|
792 | Returns a list containing all the keys associated with value \a
|
---|
793 | value, in an arbitrary order.
|
---|
794 |
|
---|
795 | This function can be slow (\l{linear time}), because QHash's
|
---|
796 | internal data structure is optimized for fast lookup by key, not
|
---|
797 | by value.
|
---|
798 | */
|
---|
799 |
|
---|
800 | /*! \fn QList<T> QHash::values() const
|
---|
801 |
|
---|
802 | Returns a list containing all the values in the hash, in an
|
---|
803 | arbitrary order. If a key is associated multiple values, all of
|
---|
804 | its values will be in the list, and not just the most recently
|
---|
805 | inserted one.
|
---|
806 |
|
---|
807 | The order is guaranteed to be the same as that used by keys().
|
---|
808 |
|
---|
809 | \sa keys(), value()
|
---|
810 | */
|
---|
811 |
|
---|
812 | /*! \fn QList<T> QHash::values(const Key &key) const
|
---|
|
---|