| 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
|
|---|
| 813 |
|
|---|
| 814 | \overload
|
|---|
| 815 |
|
|---|
| 816 | Returns a list of all the values associated with the \a key,
|
|---|
| 817 | from the most recently inserted to the least recently inserted.
|
|---|
| 818 |
|
|---|
| 819 | \sa count(), insertMulti()
|
|---|
| 820 | */
|
|---|
| 821 |
|
|---|
| 822 | /*! \fn Key QHash::key(const T &value) const
|
|---|
| 823 |
|
|---|
| 824 | Returns the first key mapped to \a value.
|
|---|
| 825 |
|
|---|
| 826 | If the hash contains no item with the \a value, the function
|
|---|
| 827 | returns a \link {default-constructed value} default-constructed
|
|---|
| 828 | key \endlink.
|
|---|
| 829 |
|
|---|
| 830 | This function can be slow (\l{linear time}), because QHash's
|
|---|
| 831 | internal data structure is optimized for fast lookup by key, not
|
|---|
| 832 | by value.
|
|---|
| 833 |
|
|---|
| 834 | \sa value(), keys()
|
|---|
| 835 | */
|
|---|
| 836 |
|
|---|
| 837 | /*!
|
|---|
| 838 | \fn Key QHash::key(const T &value, const Key &defaultKey) const
|
|---|
| 839 | \since 4.3
|
|---|
| 840 | \overload
|
|---|
| 841 |
|
|---|
| 842 | Returns the first key mapped to \a value, or \a defaultKey if the
|
|---|
| 843 | hash contains no item mapped to \a value.
|
|---|
| 844 |
|
|---|
| 845 | This function can be slow (\l{linear time}), because QHash's
|
|---|
| 846 | internal data structure is optimized for fast lookup by key, not
|
|---|
| 847 | by value.
|
|---|
| 848 | */
|
|---|
| 849 |
|
|---|
| 850 | /*! \fn int QHash::count(const Key &key) const
|
|---|
| 851 |
|
|---|
| 852 | Returns the number of items associated with the \a key.
|
|---|
| 853 |
|
|---|
| 854 | \sa contains(), insertMulti()
|
|---|
| 855 | */
|
|---|
| 856 |
|
|---|
| 857 | /*! \fn int QHash::count() const
|
|---|
| 858 |
|
|---|
| 859 | \overload
|
|---|
| 860 |
|
|---|
| 861 | Same as size().
|
|---|
| 862 | */
|
|---|
| 863 |
|
|---|
| 864 | /*! \fn QHash::iterator QHash::begin()
|
|---|
| 865 |
|
|---|
| 866 | Returns an \l{STL-style iterator} pointing to the first item in
|
|---|
| 867 | the hash.
|
|---|
| 868 |
|
|---|
| 869 | \sa constBegin(), end()
|
|---|
| 870 | */
|
|---|
| 871 |
|
|---|
| 872 | /*! \fn QHash::const_iterator QHash::begin() const
|
|---|
| 873 |
|
|---|
| 874 | \overload
|
|---|
| 875 | */
|
|---|
| 876 |
|
|---|
| 877 | /*! \fn QHash::const_iterator QHash::constBegin() const
|
|---|
| 878 |
|
|---|
| 879 | Returns a const \l{STL-style iterator} pointing to the first item
|
|---|
| 880 | in the hash.
|
|---|
| 881 |
|
|---|
| 882 | \sa begin(), constEnd()
|
|---|
| 883 | */
|
|---|
| 884 |
|
|---|
| 885 | /*! \fn QHash::iterator QHash::end()
|
|---|
| 886 |
|
|---|
| 887 | Returns an \l{STL-style iterator} pointing to the imaginary item
|
|---|
| 888 | after the last item in the hash.
|
|---|
| 889 |
|
|---|
| 890 | \sa begin(), constEnd()
|
|---|
| 891 | */
|
|---|
| 892 |
|
|---|
| 893 | /*! \fn QHash::const_iterator QHash::end() const
|
|---|
| 894 |
|
|---|
| 895 | \overload
|
|---|
| 896 | */
|
|---|
| 897 |
|
|---|
| 898 | /*! \fn QHash::const_iterator QHash::constEnd() const
|
|---|
| 899 |
|
|---|
| 900 | Returns a const \l{STL-style iterator} pointing to the imaginary
|
|---|
| 901 | item after the last item in the hash.
|
|---|
| 902 |
|
|---|
| 903 | \sa constBegin(), end()
|
|---|
| 904 | */
|
|---|
| 905 |
|
|---|
| 906 | /*! \fn QHash::iterator QHash::erase(iterator pos)
|
|---|
| 907 |
|
|---|
| 908 | Removes the (key, value) pair associated with the iterator \a pos
|
|---|
| 909 | from the hash, and returns an iterator to the next item in the
|
|---|
| 910 | hash.
|
|---|
| 911 |
|
|---|
| 912 | Unlike remove() and take(), this function never causes QHash to
|
|---|
| 913 | rehash its internal data structure. This means that it can safely
|
|---|
| 914 | be called while iterating, and won't affect the order of items in
|
|---|
| 915 | the hash. For example:
|
|---|
| 916 |
|
|---|
| 917 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 15
|
|---|
| 918 |
|
|---|
| 919 | \sa remove(), take(), find()
|
|---|
| 920 | */
|
|---|
| 921 |
|
|---|
| 922 | /*! \fn QHash::iterator QHash::find(const Key &key)
|
|---|
| 923 |
|
|---|
| 924 | Returns an iterator pointing to the item with the \a key in the
|
|---|
| 925 | hash.
|
|---|
| 926 |
|
|---|
| 927 | If the hash contains no item with the \a key, the function
|
|---|
| 928 | returns end().
|
|---|
| 929 |
|
|---|
| 930 | If the hash contains multiple items with the \a key, this
|
|---|
| 931 | function returns an iterator that points to the most recently
|
|---|
| 932 | inserted value. The other values are accessible by incrementing
|
|---|
| 933 | the iterator. For example, here's some code that iterates over all
|
|---|
| 934 | the items with the same key:
|
|---|
| 935 |
|
|---|
| 936 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 16
|
|---|
| 937 |
|
|---|
| 938 | \sa value(), values(), QMultiHash::find()
|
|---|
| 939 | */
|
|---|
| 940 |
|
|---|
| 941 | /*! \fn QHash::const_iterator QHash::find(const Key &key) const
|
|---|
| 942 |
|
|---|
| 943 | \overload
|
|---|
| 944 | */
|
|---|
| 945 |
|
|---|
| 946 | /*! \fn QHash::iterator QHash::constFind(const Key &key) const
|
|---|
| 947 | \since 4.1
|
|---|
| 948 |
|
|---|
| 949 | Returns an iterator pointing to the item with the \a key in the
|
|---|
| 950 | hash.
|
|---|
| 951 |
|
|---|
| 952 | If the hash contains no item with the \a key, the function
|
|---|
| 953 | returns constEnd().
|
|---|
| 954 |
|
|---|
| 955 | \sa find(), QMultiHash::constFind()
|
|---|
| 956 | */
|
|---|
| 957 |
|
|---|
| 958 | /*! \fn QHash::iterator QHash::insert(const Key &key, const T &value)
|
|---|
| 959 |
|
|---|
| 960 | Inserts a new item with the \a key and a value of \a value.
|
|---|
| 961 |
|
|---|
| 962 | If there is already an item with the \a key, that item's value
|
|---|
| 963 | is replaced with \a value.
|
|---|
| 964 |
|
|---|
| 965 | If there are multiple items with the \a key, the most
|
|---|
| 966 | recently inserted item's value is replaced with \a value.
|
|---|
| 967 |
|
|---|
| 968 | \sa insertMulti()
|
|---|
| 969 | */
|
|---|
| 970 |
|
|---|
| 971 | /*! \fn QHash::iterator QHash::insertMulti(const Key &key, const T &value)
|
|---|
| 972 |
|
|---|
| 973 | Inserts a new item with the \a key and a value of \a value.
|
|---|
| 974 |
|
|---|
| 975 | If there is already an item with the same key in the hash, this
|
|---|
| 976 | function will simply create a new one. (This behavior is
|
|---|
| 977 | different from insert(), which overwrites the value of an
|
|---|
| 978 | existing item.)
|
|---|
| 979 |
|
|---|
| 980 | \sa insert(), values()
|
|---|
| 981 | */
|
|---|
| 982 |
|
|---|
| 983 | /*! \fn QHash<Key, T> &QHash::unite(const QHash<Key, T> &other)
|
|---|
| 984 |
|
|---|
| 985 | Inserts all the items in the \a other hash into this hash. If a
|
|---|
| 986 | key is common to both hashes, the resulting hash will contain the
|
|---|
| 987 | key multiple times.
|
|---|
| 988 |
|
|---|
| 989 | \sa insertMulti()
|
|---|
| 990 | */
|
|---|
| 991 |
|
|---|
| 992 | /*! \fn bool QHash::empty() const
|
|---|
| 993 |
|
|---|
| 994 | This function is provided for STL compatibility. It is equivalent
|
|---|
| 995 | to isEmpty(), returning true if the hash is empty; otherwise
|
|---|
| 996 | returns false.
|
|---|
| 997 | */
|
|---|
| 998 |
|
|---|
| 999 | /*! \typedef QHash::ConstIterator
|
|---|
| 1000 |
|
|---|
| 1001 | Qt-style synonym for QHash::const_iterator.
|
|---|
| 1002 | */
|
|---|
| 1003 |
|
|---|
| 1004 | /*! \typedef QHash::Iterator
|
|---|
| 1005 |
|
|---|
| 1006 | Qt-style synonym for QHash::iterator.
|
|---|
| 1007 | */
|
|---|
| 1008 |
|
|---|
| 1009 | /*! \typedef QHash::difference_type
|
|---|
| 1010 |
|
|---|
| 1011 | Typedef for ptrdiff_t. Provided for STL compatibility.
|
|---|
| 1012 | */
|
|---|
| 1013 |
|
|---|
| 1014 | /*! \typedef QHash::key_type
|
|---|
| 1015 |
|
|---|
| 1016 | Typedef for Key. Provided for STL compatibility.
|
|---|
| 1017 | */
|
|---|
| 1018 |
|
|---|
| 1019 | /*! \typedef QHash::mapped_type
|
|---|
| 1020 |
|
|---|
| 1021 | Typedef for T. Provided for STL compatibility.
|
|---|
| 1022 | */
|
|---|
| 1023 |
|
|---|
| 1024 | /*! \typedef QHash::size_type
|
|---|
| 1025 |
|
|---|
| 1026 | Typedef for int. Provided for STL compatibility.
|
|---|
| 1027 | */
|
|---|
| 1028 |
|
|---|
| 1029 | /*! \typedef QHash::iterator::difference_type
|
|---|
| 1030 | \internal
|
|---|
| 1031 | */
|
|---|
| 1032 |
|
|---|
| 1033 | /*! \typedef QHash::iterator::iterator_category
|
|---|
| 1034 | \internal
|
|---|
| 1035 | */
|
|---|
| 1036 |
|
|---|
| 1037 | /*! \typedef QHash::iterator::pointer
|
|---|
| 1038 | \internal
|
|---|
| 1039 | */
|
|---|
| 1040 |
|
|---|
| 1041 | /*! \typedef QHash::iterator::reference
|
|---|
| 1042 | \internal
|
|---|
| 1043 | */
|
|---|
| 1044 |
|
|---|
| 1045 | /*! \typedef QHash::iterator::value_type
|
|---|
| 1046 | \internal
|
|---|
| 1047 | */
|
|---|
| 1048 |
|
|---|
| 1049 | /*! \typedef QHash::const_iterator::difference_type
|
|---|
| 1050 | \internal
|
|---|
| 1051 | */
|
|---|
| 1052 |
|
|---|
| 1053 | /*! \typedef QHash::const_iterator::iterator_category
|
|---|
| 1054 | \internal
|
|---|
| 1055 | */
|
|---|
| 1056 |
|
|---|
| 1057 | /*! \typedef QHash::const_iterator::pointer
|
|---|
| 1058 | \internal
|
|---|
| 1059 | */
|
|---|
| 1060 |
|
|---|
| 1061 | /*! \typedef QHash::const_iterator::reference
|
|---|
| 1062 | \internal
|
|---|
| 1063 | */
|
|---|
| 1064 |
|
|---|
| 1065 | /*! \typedef QHash::const_iterator::value_type
|
|---|
| 1066 | \internal
|
|---|
| 1067 | */
|
|---|
| 1068 |
|
|---|
| 1069 | /*! \class QHash::iterator
|
|---|
| 1070 | \brief The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
|
|---|
| 1071 |
|
|---|
| 1072 | QHash features both \l{STL-style iterators} and \l{Java-style
|
|---|
| 1073 | iterators}. The STL-style iterators are more low-level and more
|
|---|
| 1074 | cumbersome to use; on the other hand, they are slightly faster
|
|---|
| 1075 | and, for developers who already know STL, have the advantage of
|
|---|
| 1076 | familiarity.
|
|---|
| 1077 |
|
|---|
| 1078 | QHash\<Key, T\>::iterator allows you to iterate over a QHash (or
|
|---|
| 1079 | QMultiHash) and to modify the value (but not the key) associated
|
|---|
| 1080 | with a particular key. If you want to iterate over a const QHash,
|
|---|
| 1081 | you should use QHash::const_iterator. It is generally good
|
|---|
| 1082 | practice to use QHash::const_iterator on a non-const QHash as
|
|---|
| 1083 | well, unless you need to change the QHash through the iterator.
|
|---|
| 1084 | Const iterators are slightly faster, and can improve code
|
|---|
| 1085 | readability.
|
|---|
| 1086 |
|
|---|
| 1087 | The default QHash::iterator constructor creates an uninitialized
|
|---|
| 1088 | iterator. You must initialize it using a QHash function like
|
|---|
| 1089 | QHash::begin(), QHash::end(), or QHash::find() before you can
|
|---|
| 1090 | start iterating. Here's a typical loop that prints all the (key,
|
|---|
| 1091 | value) pairs stored in a hash:
|
|---|
| 1092 |
|
|---|
| 1093 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 17
|
|---|
| 1094 |
|
|---|
| 1095 | Unlike QMap, which orders its items by key, QHash stores its
|
|---|
| 1096 | items in an arbitrary order. The only guarantee is that items that
|
|---|
| 1097 | share the same key (because they were inserted using
|
|---|
| 1098 | QHash::insertMulti()) will appear consecutively, from the most
|
|---|
| 1099 | recently to the least recently inserted value.
|
|---|
| 1100 |
|
|---|
| 1101 | Let's see a few examples of things we can do with a
|
|---|
| 1102 | QHash::iterator that we cannot do with a QHash::const_iterator.
|
|---|
| 1103 | Here's an example that increments every value stored in the QHash
|
|---|
| 1104 | by 2:
|
|---|
| 1105 |
|
|---|
| 1106 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 18
|
|---|
| 1107 |
|
|---|
| 1108 | Here's an example that removes all the items whose key is a
|
|---|
| 1109 | string that starts with an underscore character:
|
|---|
| 1110 |
|
|---|
| 1111 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 19
|
|---|
| 1112 |
|
|---|
| 1113 | The call to QHash::erase() removes the item pointed to by the
|
|---|
| 1114 | iterator from the hash, and returns an iterator to the next item.
|
|---|
| 1115 | Here's another way of removing an item while iterating:
|
|---|
| 1116 |
|
|---|
| 1117 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 20
|
|---|
| 1118 |
|
|---|
| 1119 | It might be tempting to write code like this:
|
|---|
| 1120 |
|
|---|
| 1121 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 21
|
|---|
| 1122 |
|
|---|
| 1123 | However, this will potentially crash in \c{++i}, because \c i is
|
|---|
| 1124 | a dangling iterator after the call to erase().
|
|---|
| 1125 |
|
|---|
| 1126 | Multiple iterators can be used on the same hash. However, be
|
|---|
| 1127 | aware that any modification performed directly on the QHash has
|
|---|
| 1128 | the potential of dramatically changing the order in which the
|
|---|
| 1129 | items are stored in the hash, as they might cause QHash to rehash
|
|---|
| 1130 | its internal data structure. There is one notable exception:
|
|---|
| 1131 | QHash::erase(). This function can safely be called while
|
|---|
| 1132 | iterating, and won't affect the order of items in the hash. If you
|
|---|
| 1133 | need to keep iterators over a long period of time, we recommend
|
|---|
| 1134 | that you use QMap rather than QHash.
|
|---|
| 1135 |
|
|---|
| 1136 | \sa QHash::const_iterator, QMutableHashIterator
|
|---|
| 1137 | */
|
|---|
| 1138 |
|
|---|
| 1139 | /*! \fn QHash::iterator::operator Node *() const
|
|---|
| 1140 |
|
|---|
| 1141 | \internal
|
|---|
| 1142 | */
|
|---|
| 1143 |
|
|---|
| 1144 | /*! \fn QHash::iterator::iterator()
|
|---|
| 1145 |
|
|---|
| 1146 | Constructs an uninitialized iterator.
|
|---|
| 1147 |
|
|---|
| 1148 | Functions like key(), value(), and operator++() must not be
|
|---|
| 1149 | called on an uninitialized iterator. Use operator=() to assign a
|
|---|
| 1150 | value to it before using it.
|
|---|
| 1151 |
|
|---|
| 1152 | \sa QHash::begin() QHash::end()
|
|---|
| 1153 | */
|
|---|
| 1154 |
|
|---|
| 1155 | /*! \fn QHash::iterator::iterator(void *node)
|
|---|
| 1156 |
|
|---|
| 1157 | \internal
|
|---|
| 1158 | */
|
|---|
| 1159 |
|
|---|
| 1160 | /*! \fn const Key &QHash::iterator::key() const
|
|---|
| 1161 |
|
|---|
| 1162 | Returns the current item's key as a const reference.
|
|---|
| 1163 |
|
|---|
| 1164 | There is no direct way of changing an item's key through an
|
|---|
| 1165 | iterator, although it can be done by calling QHash::erase()
|
|---|
| 1166 | followed by QHash::insert() or QHash::insertMulti().
|
|---|
| 1167 |
|
|---|
| 1168 | \sa value()
|
|---|
| 1169 | */
|
|---|
| 1170 |
|
|---|
| 1171 | /*! \fn T &QHash::iterator::value() const
|
|---|
| 1172 |
|
|---|
| 1173 | Returns a modifiable reference to the current item's value.
|
|---|
| 1174 |
|
|---|
| 1175 | You can change the value of an item by using value() on
|
|---|
| 1176 | the left side of an assignment, for example:
|
|---|
| 1177 |
|
|---|
| 1178 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 22
|
|---|
| 1179 |
|
|---|
| 1180 | \sa key(), operator*()
|
|---|
| 1181 | */
|
|---|
| 1182 |
|
|---|
| 1183 | /*! \fn T &QHash::iterator::operator*() const
|
|---|
| 1184 |
|
|---|
| 1185 | Returns a modifiable reference to the current item's value.
|
|---|
| 1186 |
|
|---|
| 1187 | Same as value().
|
|---|
| 1188 |
|
|---|
| 1189 | \sa key()
|
|---|
| 1190 | */
|
|---|
| 1191 |
|
|---|
| 1192 | /*! \fn T *QHash::iterator::operator->() const
|
|---|
| 1193 |
|
|---|
| 1194 | Returns a pointer to the current item's value.
|
|---|
| 1195 |
|
|---|
| 1196 | \sa value()
|
|---|
| 1197 | */
|
|---|
| 1198 |
|
|---|
| 1199 | /*!
|
|---|
| 1200 | \fn bool QHash::iterator::operator==(const iterator &other) const
|
|---|
| 1201 | \fn bool QHash::iterator::operator==(const const_iterator &other) const
|
|---|
| 1202 |
|
|---|
| 1203 | Returns true if \a other points to the same item as this
|
|---|
| 1204 | iterator; otherwise returns false.
|
|---|
| 1205 |
|
|---|
| 1206 | \sa operator!=()
|
|---|
| 1207 | */
|
|---|
| 1208 |
|
|---|
| 1209 | /*!
|
|---|
| 1210 | \fn bool QHash::iterator::operator!=(const iterator &other) const
|
|---|
| 1211 | \fn bool QHash::iterator::operator!=(const const_iterator &other) const
|
|---|
| 1212 |
|
|---|
| 1213 | Returns true if \a other points to a different item than this
|
|---|
| 1214 | iterator; otherwise returns false.
|
|---|
| 1215 |
|
|---|
| 1216 | \sa operator==()
|
|---|
| 1217 | */
|
|---|
| 1218 |
|
|---|
| 1219 | /*!
|
|---|
| 1220 | \fn QHash::iterator &QHash::iterator::operator++()
|
|---|
| 1221 |
|
|---|
| 1222 | The prefix ++ operator (\c{++i}) advances the iterator to the
|
|---|
| 1223 | next item in the hash and returns an iterator to the new current
|
|---|
| 1224 | item.
|
|---|
| 1225 |
|
|---|
| 1226 | Calling this function on QHash::end() leads to undefined results.
|
|---|
| 1227 |
|
|---|
| 1228 | \sa operator--()
|
|---|
| 1229 | */
|
|---|
| 1230 |
|
|---|
| 1231 | /*! \fn QHash::iterator QHash::iterator::operator++(int)
|
|---|
| 1232 |
|
|---|
| 1233 | \overload
|
|---|
| 1234 |
|
|---|
| 1235 | The postfix ++ operator (\c{i++}) advances the iterator to the
|
|---|
| 1236 | next item in the hash and returns an iterator to the previously
|
|---|
| 1237 | current item.
|
|---|
| 1238 | */
|
|---|
| 1239 |
|
|---|
| 1240 | /*!
|
|---|
| 1241 | \fn QHash::iterator &QHash::iterator::operator--()
|
|---|
| 1242 |
|
|---|
| 1243 | The prefix -- operator (\c{--i}) makes the preceding item
|
|---|
| 1244 | current and returns an iterator pointing to the new current item.
|
|---|
| 1245 |
|
|---|
| 1246 | Calling this function on QHash::begin() leads to undefined
|
|---|
| 1247 | results.
|
|---|
| 1248 |
|
|---|
| 1249 | \sa operator++()
|
|---|
| 1250 | */
|
|---|
| 1251 |
|
|---|
| 1252 | /*!
|
|---|
| 1253 | \fn QHash::iterator QHash::iterator::operator--(int)
|
|---|
| 1254 |
|
|---|
| 1255 | \overload
|
|---|
| 1256 |
|
|---|
| 1257 | The postfix -- operator (\c{i--}) makes the preceding item
|
|---|
| 1258 | current and returns an iterator pointing to the previously
|
|---|
| 1259 | current item.
|
|---|
| 1260 | */
|
|---|
| 1261 |
|
|---|
| 1262 | /*! \fn QHash::iterator QHash::iterator::operator+(int j) const
|
|---|
| 1263 |
|
|---|
| 1264 | Returns an iterator to the item at \a j positions forward from
|
|---|
| 1265 | this iterator. (If \a j is negative, the iterator goes backward.)
|
|---|
| 1266 |
|
|---|
| 1267 | This operation can be slow for large \a j values.
|
|---|
| 1268 |
|
|---|
| 1269 | \sa operator-()
|
|---|
| 1270 |
|
|---|
| 1271 | */
|
|---|
| 1272 |
|
|---|
| 1273 | /*! \fn QHash::iterator QHash::iterator::operator-(int j) const
|
|---|
| 1274 |
|
|---|
| 1275 | Returns an iterator to the item at \a j positions backward from
|
|---|
| 1276 | this iterator. (If \a j is negative, the iterator goes forward.)
|
|---|
| 1277 |
|
|---|
| 1278 | This operation can be slow for large \a j values.
|
|---|
| 1279 |
|
|---|
| 1280 | \sa operator+()
|
|---|
| 1281 | */
|
|---|
| 1282 |
|
|---|
| 1283 | /*! \fn QHash::iterator &QHash::iterator::operator+=(int j)
|
|---|
| 1284 |
|
|---|
| 1285 | Advances the iterator by \a j items. (If \a j is negative, the
|
|---|
| 1286 | iterator goes backward.)
|
|---|
| 1287 |
|
|---|
| 1288 | \sa operator-=(), operator+()
|
|---|
| 1289 | */
|
|---|
| 1290 |
|
|---|
| 1291 | /*! \fn QHash::iterator &QHash::iterator::operator-=(int j)
|
|---|
| 1292 |
|
|---|
| 1293 | Makes the iterator go back by \a j items. (If \a j is negative,
|
|---|
| 1294 | the iterator goes forward.)
|
|---|
| 1295 |
|
|---|
| 1296 | \sa operator+=(), operator-()
|
|---|
| 1297 | */
|
|---|
| 1298 |
|
|---|
| 1299 | /*! \class QHash::const_iterator
|
|---|
| 1300 | \brief The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash.
|
|---|
| 1301 |
|
|---|
| 1302 | QHash features both \l{STL-style iterators} and \l{Java-style
|
|---|
| 1303 | iterators}. The STL-style iterators are more low-level and more
|
|---|
| 1304 | cumbersome to use; on the other hand, they are slightly faster
|
|---|
| 1305 | and, for developers who already know STL, have the advantage of
|
|---|
| 1306 | familiarity.
|
|---|
| 1307 |
|
|---|
| 1308 | QHash\<Key, T\>::const_iterator allows you to iterate over a
|
|---|
| 1309 | QHash (or a QMultiHash). If you want to modify the QHash as you
|
|---|
| 1310 | iterate over it, you must use QHash::iterator instead. It is
|
|---|
| 1311 | generally good practice to use QHash::const_iterator on a
|
|---|
| 1312 | non-const QHash as well, unless you need to change the QHash
|
|---|
| 1313 | through the iterator. Const iterators are slightly faster, and
|
|---|
| 1314 | can improve code readability.
|
|---|
| 1315 |
|
|---|
| 1316 | The default QHash::const_iterator constructor creates an
|
|---|
| 1317 | uninitialized iterator. You must initialize it using a QHash
|
|---|
| 1318 | function like QHash::constBegin(), QHash::constEnd(), or
|
|---|
| 1319 | QHash::find() before you can start iterating. Here's a typical
|
|---|
| 1320 | loop that prints all the (key, value) pairs stored in a hash:
|
|---|
| 1321 |
|
|---|
| 1322 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 23
|
|---|
| 1323 |
|
|---|
| 1324 | Unlike QMap, which orders its items by key, QHash stores its
|
|---|
| 1325 | items in an arbitrary order. The only guarantee is that items that
|
|---|
| 1326 | share the same key (because they were inserted using
|
|---|
| 1327 | QHash::insertMulti()) will appear consecutively, from the most
|
|---|
| 1328 | recently to the least recently inserted value.
|
|---|
| 1329 |
|
|---|
| 1330 | Multiple iterators can be used on the same hash. However, be aware
|
|---|
| 1331 | that any modification performed directly on the QHash has the
|
|---|
| 1332 | potential of dramatically changing the order in which the items
|
|---|
| 1333 | are stored in the hash, as they might cause QHash to rehash its
|
|---|
| 1334 | internal data structure. If you need to keep iterators over a long
|
|---|
| 1335 | period of time, we recommend that you use QMap rather than QHash.
|
|---|
| 1336 |
|
|---|
| 1337 | \sa QHash::iterator, QHashIterator
|
|---|
| 1338 | */
|
|---|
| 1339 |
|
|---|
| 1340 | /*! \fn QHash::const_iterator::operator Node *() const
|
|---|
| 1341 |
|
|---|
| 1342 | \internal
|
|---|
| 1343 | */
|
|---|
| 1344 |
|
|---|
| 1345 | /*! \fn QHash::const_iterator::const_iterator()
|
|---|
| 1346 |
|
|---|
| 1347 | Constructs an uninitialized iterator.
|
|---|
| 1348 |
|
|---|
| 1349 | Functions like key(), value(), and operator++() must not be
|
|---|
| 1350 | called on an uninitialized iterator. Use operator=() to assign a
|
|---|
| 1351 | value to it before using it.
|
|---|
| 1352 |
|
|---|
| 1353 | \sa QHash::constBegin() QHash::constEnd()
|
|---|
| 1354 | */
|
|---|
| 1355 |
|
|---|
| 1356 | /*! \fn QHash::const_iterator::const_iterator(void *node)
|
|---|
| 1357 |
|
|---|
| 1358 | \internal
|
|---|
| 1359 | */
|
|---|
| 1360 |
|
|---|
| 1361 | /*! \fn QHash::const_iterator::const_iterator(const iterator &other)
|
|---|
| 1362 |
|
|---|
| 1363 | Constructs a copy of \a other.
|
|---|
| 1364 | */
|
|---|
| 1365 |
|
|---|
| 1366 | /*! \fn const Key &QHash::const_iterator::key() const
|
|---|
| 1367 |
|
|---|
| 1368 | Returns the current item's key.
|
|---|
| 1369 |
|
|---|
| 1370 | \sa value()
|
|---|
| 1371 | */
|
|---|
| 1372 |
|
|---|
| 1373 | /*! \fn const T &QHash::const_iterator::value() const
|
|---|
| 1374 |
|
|---|
| 1375 | Returns the current item's value.
|
|---|
| 1376 |
|
|---|
| 1377 | \sa key(), operator*()
|
|---|
| 1378 | */
|
|---|
| 1379 |
|
|---|
| 1380 | /*! \fn const T &QHash::const_iterator::operator*() const
|
|---|
| 1381 |
|
|---|
| 1382 | Returns the current item's value.
|
|---|
| 1383 |
|
|---|
| 1384 | Same as value().
|
|---|
| 1385 |
|
|---|
| 1386 | \sa key()
|
|---|
| 1387 | */
|
|---|
| 1388 |
|
|---|
| 1389 | /*! \fn const T *QHash::const_iterator::operator->() const
|
|---|
| 1390 |
|
|---|
| 1391 | Returns a pointer to the current item's value.
|
|---|
| 1392 |
|
|---|
| 1393 | \sa value()
|
|---|
| 1394 | */
|
|---|
| 1395 |
|
|---|
| 1396 | /*! \fn bool QHash::const_iterator::operator==(const const_iterator &other) const
|
|---|
| 1397 |
|
|---|
| 1398 | Returns true if \a other points to the same item as this
|
|---|
| 1399 | iterator; otherwise returns false.
|
|---|
| 1400 |
|
|---|
| 1401 | \sa operator!=()
|
|---|
| 1402 | */
|
|---|
| 1403 |
|
|---|
| 1404 | /*! \fn bool QHash::const_iterator::operator!=(const const_iterator &other) const
|
|---|
| 1405 |
|
|---|
| 1406 | Returns true if \a other points to a different item than this
|
|---|
| 1407 | iterator; otherwise returns false.
|
|---|
| 1408 |
|
|---|
| 1409 | \sa operator==()
|
|---|
| 1410 | */
|
|---|
| 1411 |
|
|---|
| 1412 | /*!
|
|---|
| 1413 | \fn QHash::const_iterator &QHash::const_iterator::operator++()
|
|---|
| 1414 |
|
|---|
| 1415 | The prefix ++ operator (\c{++i}) advances the iterator to the
|
|---|
| 1416 | next item in the hash and returns an iterator to the new current
|
|---|
| 1417 | item.
|
|---|
| 1418 |
|
|---|
| 1419 | Calling this function on QHash::end() leads to undefined results.
|
|---|
| 1420 |
|
|---|
| 1421 | \sa operator--()
|
|---|
| 1422 | */
|
|---|
| 1423 |
|
|---|
| 1424 | /*! \fn QHash::const_iterator QHash::const_iterator::operator++(int)
|
|---|
| 1425 |
|
|---|
| 1426 | \overload
|
|---|
| 1427 |
|
|---|
| 1428 | The postfix ++ operator (\c{i++}) advances the iterator to the
|
|---|
| 1429 | next item in the hash and returns an iterator to the previously
|
|---|
| 1430 | current item.
|
|---|
| 1431 | */
|
|---|
| 1432 |
|
|---|
| 1433 | /*! \fn QHash::const_iterator &QHash::const_iterator::operator--()
|
|---|
| 1434 |
|
|---|
| 1435 | The prefix -- operator (\c{--i}) makes the preceding item
|
|---|
| 1436 | current and returns an iterator pointing to the new current item.
|
|---|
| 1437 |
|
|---|
| 1438 | Calling this function on QHash::begin() leads to undefined
|
|---|
| 1439 | results.
|
|---|
| 1440 |
|
|---|
| 1441 | \sa operator++()
|
|---|
| 1442 | */
|
|---|
| 1443 |
|
|---|
| 1444 | /*! \fn QHash::const_iterator QHash::const_iterator::operator--(int)
|
|---|
| 1445 |
|
|---|
| 1446 | \overload
|
|---|
| 1447 |
|
|---|
| 1448 | The postfix -- operator (\c{i--}) makes the preceding item
|
|---|
| 1449 | current and returns an iterator pointing to the previously
|
|---|
| 1450 | current item.
|
|---|
| 1451 | */
|
|---|
| 1452 |
|
|---|
| 1453 | /*! \fn QHash::const_iterator QHash::const_iterator::operator+(int j) const
|
|---|
| 1454 |
|
|---|
| 1455 | Returns an iterator to the item at \a j positions forward from
|
|---|
| 1456 | this iterator. (If \a j is negative, the iterator goes backward.)
|
|---|
| 1457 |
|
|---|
| 1458 | This operation can be slow for large \a j values.
|
|---|
| 1459 |
|
|---|
| 1460 | \sa operator-()
|
|---|
| 1461 | */
|
|---|
| 1462 |
|
|---|
| 1463 | /*! \fn QHash::const_iterator QHash::const_iterator::operator-(int j) const
|
|---|
| 1464 |
|
|---|
| 1465 | Returns an iterator to the item at \a j positions backward from
|
|---|
| 1466 | this iterator. (If \a j is negative, the iterator goes forward.)
|
|---|
| 1467 |
|
|---|
| 1468 | This operation can be slow for large \a j values.
|
|---|
| 1469 |
|
|---|
| 1470 | \sa operator+()
|
|---|
| 1471 | */
|
|---|
| 1472 |
|
|---|
| 1473 | /*! \fn QHash::const_iterator &QHash::const_iterator::operator+=(int j)
|
|---|
| 1474 |
|
|---|
| 1475 | Advances the iterator by \a j items. (If \a j is negative, the
|
|---|
| 1476 | iterator goes backward.)
|
|---|
| 1477 |
|
|---|
| 1478 | This operation can be slow for large \a j values.
|
|---|
| 1479 |
|
|---|
| 1480 | \sa operator-=(), operator+()
|
|---|
| 1481 | */
|
|---|
| 1482 |
|
|---|
| 1483 | /*! \fn QHash::const_iterator &QHash::const_iterator::operator-=(int j)
|
|---|
| 1484 |
|
|---|
| 1485 | Makes the iterator go back by \a j items. (If \a j is negative,
|
|---|
| 1486 | the iterator goes forward.)
|
|---|
| 1487 |
|
|---|
| 1488 | This operation can be slow for large \a j values.
|
|---|
| 1489 |
|
|---|
| 1490 | \sa operator+=(), operator-()
|
|---|
| 1491 | */
|
|---|
| 1492 |
|
|---|
| 1493 | /*! \fn uint qHash(char key)
|
|---|
| 1494 | \relates QHash
|
|---|
| 1495 |
|
|---|
| 1496 | Returns the hash value for the \a key.
|
|---|
| 1497 | */
|
|---|
| 1498 |
|
|---|
| 1499 | /*! \fn uint qHash(uchar key)
|
|---|
| 1500 | \relates QHash
|
|---|
| 1501 | \overload
|
|---|
| 1502 |
|
|---|
| 1503 | Returns the hash value for the \a key.
|
|---|
| 1504 | */
|
|---|
| 1505 |
|
|---|
| 1506 | /*! \fn uint qHash(signed char key)
|
|---|
| 1507 | \relates QHash
|
|---|
| 1508 | \overload
|
|---|
| 1509 |
|
|---|
| 1510 | Returns the hash value for the \a key.
|
|---|
| 1511 | */
|
|---|
| 1512 |
|
|---|
| 1513 | /*! \fn uint qHash(ushort key)
|
|---|
| 1514 | \relates QHash
|
|---|
| 1515 | \overload
|
|---|
| 1516 |
|
|---|
| 1517 | Returns the hash value for the \a key.
|
|---|
| 1518 | */
|
|---|
| 1519 |
|
|---|
| 1520 | /*! \fn uint qHash(short key)
|
|---|
| 1521 | \relates QHash
|
|---|
| 1522 | \overload
|
|---|
| 1523 |
|
|---|
| 1524 | Returns the hash value for the \a key.
|
|---|
| 1525 | */
|
|---|
| 1526 |
|
|---|
| 1527 | /*! \fn uint qHash(uint key)
|
|---|
| 1528 | \relates QHash
|
|---|
| 1529 | \overload
|
|---|
| 1530 |
|
|---|
| 1531 | Returns the hash value for the \a key.
|
|---|
| 1532 | */
|
|---|
| 1533 |
|
|---|
| 1534 | /*! \fn uint qHash(int key)
|
|---|
| 1535 | \relates QHash
|
|---|
| 1536 | \overload
|
|---|
| 1537 |
|
|---|
| 1538 | Returns the hash value for the \a key.
|
|---|
| 1539 | */
|
|---|
| 1540 |
|
|---|
| 1541 | /*! \fn uint qHash(ulong key)
|
|---|
| 1542 | \relates QHash
|
|---|
| 1543 | \overload
|
|---|
| 1544 |
|
|---|
| 1545 | Returns the hash value for the \a key.
|
|---|
| 1546 | */
|
|---|
| 1547 |
|
|---|
| 1548 | /*! \fn uint qHash(long key)
|
|---|
| 1549 | \relates QHash
|
|---|
| 1550 | \overload
|
|---|
| 1551 |
|
|---|
| 1552 | Returns the hash value for the \a key.
|
|---|
| 1553 | */
|
|---|
| 1554 |
|
|---|
| 1555 | /*! \fn uint qHash(quint64 key)
|
|---|
| 1556 | \relates QHash
|
|---|
| 1557 | \overload
|
|---|
| 1558 |
|
|---|
| 1559 | Returns the hash value for the \a key.
|
|---|
| 1560 | */
|
|---|
| 1561 |
|
|---|
| 1562 | /*! \fn uint qHash(qint64 key)
|
|---|
| 1563 | \relates QHash
|
|---|
| 1564 | \overload
|
|---|
| 1565 |
|
|---|
| 1566 | Returns the hash value for the \a key.
|
|---|
| 1567 | */
|
|---|
| 1568 |
|
|---|
| 1569 | /*! \fn uint qHash(QChar key)
|
|---|
| 1570 | \relates QHash
|
|---|
| 1571 | \overload
|
|---|
| 1572 |
|
|---|
| 1573 | Returns the hash value for the \a key.
|
|---|
| 1574 | */
|
|---|
| 1575 |
|
|---|
| 1576 | /*! \fn uint qHash(const QByteArray &key)
|
|---|
| 1577 | \fn uint qHash(const QBitArray &key)
|
|---|
| 1578 | \relates QHash
|
|---|
| 1579 | \overload
|
|---|
| 1580 |
|
|---|
| 1581 | Returns the hash value for the \a key.
|
|---|
| 1582 | */
|
|---|
| 1583 |
|
|---|
| 1584 | /*! \fn uint qHash(const QString &key)
|
|---|
| 1585 | \relates QHash
|
|---|
| 1586 | \overload
|
|---|
| 1587 |
|
|---|
| 1588 | Returns the hash value for the \a key.
|
|---|
| 1589 | */
|
|---|
| 1590 |
|
|---|
| 1591 | /*! \fn uint qHash(const T *key)
|
|---|
| 1592 | \relates QHash
|
|---|
| 1593 | \overload
|
|---|
| 1594 |
|
|---|
| 1595 | Returns the hash value for the \a key.
|
|---|
| 1596 | */
|
|---|
| 1597 |
|
|---|
| 1598 | /*!
|
|---|
| 1599 | \fn uint qHash(const QPair<T1, T2> &key)
|
|---|
| 1600 | \relates QHash
|
|---|
| 1601 | \since 4.3
|
|---|
| 1602 |
|
|---|
| 1603 | Returns the hash value for the \a key.
|
|---|
| 1604 |
|
|---|
| 1605 | Types \c T1 and \c T2 must be supported by qHash().
|
|---|
| 1606 | */
|
|---|
| 1607 |
|
|---|
| 1608 | /*! \fn QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
|
|---|
| 1609 | \relates QHash
|
|---|
| 1610 |
|
|---|
| 1611 | Writes the hash \a hash to stream \a out.
|
|---|
| 1612 |
|
|---|
| 1613 | This function requires the key and value types to implement \c
|
|---|
| 1614 | operator<<().
|
|---|
| 1615 |
|
|---|
| 1616 | \sa {Format of the QDataStream operators}
|
|---|
| 1617 | */
|
|---|
| 1618 |
|
|---|
| 1619 | /*! \fn QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
|
|---|
| 1620 | \relates QHash
|
|---|
| 1621 |
|
|---|
| 1622 | Reads a hash from stream \a in into \a hash.
|
|---|
| 1623 |
|
|---|
| 1624 | This function requires the key and value types to implement \c
|
|---|
| 1625 | operator>>().
|
|---|
| 1626 |
|
|---|
| 1627 | \sa {Format of the QDataStream operators}
|
|---|
| 1628 | */
|
|---|
| 1629 |
|
|---|
| 1630 | /*! \class QMultiHash
|
|---|
| 1631 | \brief The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.
|
|---|
| 1632 |
|
|---|
| 1633 | \ingroup tools
|
|---|
| 1634 | \ingroup shared
|
|---|
| 1635 | \mainclass
|
|---|
| 1636 | \reentrant
|
|---|
| 1637 |
|
|---|
| 1638 | QMultiHash\<Key, T\> is one of Qt's generic \l{container classes}.
|
|---|
| 1639 | It inherits QHash and extends it with a few convenience functions
|
|---|
| 1640 | that make it more suitable than QHash for storing multi-valued
|
|---|
| 1641 | hashes. A multi-valued hash is a hash that allows multiple values
|
|---|
| 1642 | with the same key; QHash normally doesn't allow that, unless you
|
|---|
| 1643 | call QHash::insertMulti().
|
|---|
| 1644 |
|
|---|
| 1645 | Because QMultiHash inherits QHash, all of QHash's functionality also
|
|---|
| 1646 | applies to QMultiHash. For example, you can use isEmpty() to test
|
|---|
| 1647 | whether the hash is empty, and you can traverse a QMultiHash using
|
|---|
| 1648 | QHash's iterator classes (for example, QHashIterator). But in
|
|---|
| 1649 | addition, it provides an insert() function that corresponds to
|
|---|
| 1650 | QHash::insertMulti(), and a replace() function that corresponds to
|
|---|
| 1651 | QHash::insert(). It also provides convenient operator+() and
|
|---|
| 1652 | operator+=().
|
|---|
| 1653 |
|
|---|
| 1654 | Example:
|
|---|
| 1655 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 24
|
|---|
| 1656 |
|
|---|
| 1657 | Unlike QHash, QMultiHash provides no operator[]. Use value() or
|
|---|
| 1658 | replace() if you want to access the most recently inserted item
|
|---|
| 1659 | with a certain key.
|
|---|
| 1660 |
|
|---|
| 1661 | If you want to retrieve all the values for a single key, you can
|
|---|
| 1662 | use values(const Key &key), which returns a QList<T>:
|
|---|
| 1663 |
|
|---|
| 1664 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 25
|
|---|
| 1665 |
|
|---|
| 1666 | The items that share the same key are available from most
|
|---|
| 1667 | recently to least recently inserted.
|
|---|
| 1668 |
|
|---|
| 1669 | A more efficient approach is to call find() to get
|
|---|
| 1670 | the STL-style iterator for the first item with a key and iterate from
|
|---|
| 1671 | there:
|
|---|
| 1672 |
|
|---|
| 1673 | \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 26
|
|---|
| 1674 |
|
|---|
| 1675 | QMultiHash's key and value data types must be \l{assignable data
|
|---|
| 1676 | types}. You cannot, for example, store a QWidget as a value;
|
|---|
| 1677 | instead, store a QWidget *. In addition, QMultiHash's key type
|
|---|
| 1678 | must provide operator==(), and there must also be a global
|
|---|
| 1679 | qHash() function that returns a hash value for an argument of the
|
|---|
| 1680 | key's type. See the QHash documentation for details.
|
|---|
| 1681 |
|
|---|
| 1682 | \sa QHash, QHashIterator, QMutableHashIterator, QMultiMap
|
|---|
| 1683 | */
|
|---|
| 1684 |
|
|---|
| 1685 | /*! \fn QMultiHash::QMultiHash()
|
|---|
| 1686 |
|
|---|
| 1687 | Constructs an empty hash.
|
|---|
| 1688 | */
|
|---|
| 1689 |
|
|---|
| 1690 | /*! \fn QMultiHash::QMultiHash(const QHash<Key, T> &other)
|
|---|
| 1691 |
|
|---|
| 1692 | Constructs a copy of \a other (which can be a QHash or a
|
|---|
| 1693 | QMultiHash).
|
|---|
| 1694 |
|
|---|
| 1695 | \sa operator=()
|
|---|
| 1696 | */
|
|---|
| 1697 |
|
|---|
| 1698 | /*! \fn QMultiHash::iterator QMultiHash::replace(const Key &key, const T &value)
|
|---|
| 1699 |
|
|---|
| 1700 | Inserts a new item with the \a key and a value of \a value.
|
|---|
| 1701 |
|
|---|
| 1702 | If there is already an item with the \a key, that item's value
|
|---|
| 1703 | is replaced with \a value.
|
|---|
| 1704 |
|
|---|
| 1705 | If there are multiple items with the \a key, the most
|
|---|
| 1706 | recently inserted item's value is replaced with \a value.
|
|---|
| 1707 |
|
|---|
| 1708 | \sa insert()
|
|---|
| 1709 | */
|
|---|
| 1710 |
|
|---|
| 1711 | /*! \fn QMultiHash::iterator QMultiHash::insert(const Key &key, const T &value)
|
|---|
| 1712 |
|
|---|
| 1713 | Inserts a new item with the \a key and a value of \a value.
|
|---|
| 1714 |
|
|---|
| 1715 | If there is already an item with the same key in the hash, this
|
|---|
| 1716 | function will simply create a new one. (This behavior is
|
|---|
| 1717 | different from replace(), which overwrites the value of an
|
|---|
| 1718 | existing item.)
|
|---|
| 1719 |
|
|---|
| 1720 | \sa replace()
|
|---|
| 1721 | */
|
|---|
| 1722 |
|
|---|
| 1723 | /*! \fn QMultiHash &QMultiHash::operator+=(const QMultiHash &other)
|
|---|
| 1724 |
|
|---|
| 1725 | Inserts all the items in the \a other hash into this hash
|
|---|
| 1726 | and returns a reference to this hash.
|
|---|
| 1727 |
|
|---|
| 1728 | \sa insert()
|
|---|
| 1729 | */
|
|---|
| 1730 |
|
|---|
| 1731 | /*! \fn QMultiHash QMultiHash::operator+(const QMultiHash &other) const
|
|---|
| 1732 |
|
|---|
| 1733 | Returns a hash that contains all the items in this hash in
|
|---|
| 1734 | addition to all the items in \a other. If a key is common to both
|
|---|
| 1735 | hashes, the resulting hash will contain the key multiple times.
|
|---|
| 1736 |
|
|---|
| 1737 | \sa operator+=()
|
|---|
| 1738 | */
|
|---|
| 1739 |
|
|---|
| 1740 | /*!
|
|---|
| 1741 | \fn bool QMultiHash::contains(const Key &key, const T &value) const
|
|---|
| 1742 | \since 4.3
|
|---|
| 1743 |
|
|---|
| 1744 | Returns true if the hash contains an item with the \a key and
|
|---|
| 1745 | \a value; otherwise returns false.
|
|---|
| 1746 |
|
|---|
| 1747 | \sa QHash::contains()
|
|---|
| 1748 | */
|
|---|
| 1749 |
|
|---|
| 1750 | /*!
|
|---|
| 1751 | \fn bool QMultiHash::contains(const Key &key) const
|
|---|
| 1752 | \overload
|
|---|
| 1753 | \sa QHash::contains()
|
|---|
| 1754 | */
|
|---|
| 1755 |
|
|---|
| 1756 | /*!
|
|---|
| 1757 | \fn int QMultiHash::remove(const Key &key, const T &value)
|
|---|
| 1758 | \since 4.3
|
|---|
| 1759 |
|
|---|
| 1760 | Removes all the items that have the \a key and the value \a
|
|---|
| 1761 | value from the hash. Returns the number of items removed.
|
|---|
| 1762 |
|
|---|
| 1763 | \sa QHash::remove()
|
|---|
| 1764 | */
|
|---|
| 1765 |
|
|---|
| 1766 | /*!
|
|---|
| 1767 | \fn int QMultiHash::remove(const Key &key)
|
|---|
| 1768 | \overload
|
|---|
| 1769 | \sa QHash::remove()
|
|---|
| 1770 | */
|
|---|
| 1771 |
|
|---|
| 1772 | /*!
|
|---|
| 1773 | \fn int QMultiHash::count(const Key &key, const T &value) const
|
|---|
| 1774 | \since 4.3
|
|---|
| 1775 |
|
|---|
| 1776 | Returns the number of items with the \a key and \a value.
|
|---|
| 1777 |
|
|---|
| 1778 | \sa QHash::count()
|
|---|
| 1779 | */
|
|---|
| 1780 |
|
|---|
| 1781 | /*!
|
|---|
| 1782 | \fn int QMultiHash::count(const Key &key) const
|
|---|
| 1783 | \overload
|
|---|
| 1784 | \sa QHash::count()
|
|---|
| 1785 | */
|
|---|
| 1786 |
|
|---|
| 1787 | /*!
|
|---|
| 1788 | \fn int QMultiHash::count() const
|
|---|
| 1789 | \overload
|
|---|
| 1790 | \sa QHash::count()
|
|---|
| 1791 | */
|
|---|
| 1792 |
|
|---|
| 1793 | /*!
|
|---|
| 1794 | \fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key, const T &value)
|
|---|
| 1795 | \since 4.3
|
|---|
| 1796 |
|
|---|
| 1797 | Returns an iterator pointing to the item with the \a key and \a value.
|
|---|
| 1798 | If the hash contains no such item, the function returns end().
|
|---|
| 1799 |
|
|---|
| 1800 | If the hash contains multiple items with the \a key and \a value, the
|
|---|
| 1801 | iterator returned points to the most recently inserted item.
|
|---|
| 1802 |
|
|---|
| 1803 | \sa QHash::find()
|
|---|
| 1804 | */
|
|---|
| 1805 |
|
|---|
| 1806 | /*!
|
|---|
| 1807 | \fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key)
|
|---|
| 1808 | \overload
|
|---|
| 1809 | \sa QHash::find()
|
|---|
| 1810 | */
|
|---|
| 1811 |
|
|---|
| 1812 | /*!
|
|---|
| 1813 | \fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key, const T &value) const
|
|---|
| 1814 | \since 4.3
|
|---|
| 1815 | \overload
|
|---|
| 1816 | */
|
|---|
| 1817 |
|
|---|
| 1818 | /*!
|
|---|
| 1819 | \fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key) const
|
|---|
| 1820 | \overload
|
|---|
| 1821 | \sa QHash::find()
|
|---|
| 1822 | */
|
|---|
| 1823 |
|
|---|
| 1824 | /*!
|
|---|
| 1825 | \fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key, const T &value) const
|
|---|
| 1826 | \since 4.3
|
|---|
| 1827 |
|
|---|
| 1828 | Returns an iterator pointing to the item with the \a key and the
|
|---|
| 1829 | \a value in the hash.
|
|---|
| 1830 |
|
|---|
| 1831 | If the hash contains no such item, the function returns
|
|---|
| 1832 | constEnd().
|
|---|
| 1833 |
|
|---|
| 1834 | \sa QHash::constFind()
|
|---|
| 1835 | */
|
|---|
| 1836 |
|
|---|
| 1837 | /*!
|
|---|
| 1838 | \fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key) const
|
|---|
| 1839 | \overload
|
|---|
| 1840 | \sa QHash::constFind()
|
|---|
| 1841 | */
|
|---|
| 1842 |
|
|---|
| 1843 | QT_END_NAMESPACE
|
|---|