/src/corelib/tools/qhash.cpp

https://bitbucket.org/kyanha/qt · C++ · 1914 lines · 317 code · 193 blank · 1404 comment · 59 complexity · 0ca55a5498ace9721bb7abf266c736e1 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
  4. ** Contact: http://www.qt-project.org/legal
  5. **
  6. ** This file is part of the QtCore module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and Digia. For licensing terms and
  14. ** conditions see http://qt.digia.com/licensing. For further information
  15. ** use the contact form at http://qt.digia.com/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 2.1 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 2.1 requirements
  23. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  24. **
  25. ** In addition, as a special exception, Digia gives you certain additional
  26. ** rights. These rights are described in the Digia Qt LGPL Exception
  27. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  28. **
  29. ** GNU General Public License Usage
  30. ** Alternatively, this file may be used under the terms of the GNU
  31. ** General Public License version 3.0 as published by the Free Software
  32. ** Foundation and appearing in the file LICENSE.GPL included in the
  33. ** packaging of this file. Please review the following information to
  34. ** ensure the GNU General Public License version 3.0 requirements will be
  35. ** met: http://www.gnu.org/copyleft/gpl.html.
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qhash.h"
  42. #ifdef truncate
  43. #undef truncate
  44. #endif
  45. #include <qbitarray.h>
  46. #include <qstring.h>
  47. #include <stdlib.h>
  48. #ifdef QT_QHASH_DEBUG
  49. #include <qstring.h>
  50. #endif
  51. QT_BEGIN_NAMESPACE
  52. // ### Qt 5: see tests/benchmarks/corelib/tools/qhash/qhash_string.cpp
  53. // Hashing of the whole string is a waste of cycles.
  54. /*
  55. These functions are based on Peter J. Weinberger's hash function
  56. (from the Dragon Book). The constant 24 in the original function
  57. was replaced with 23 to produce fewer collisions on input such as
  58. "a", "aa", "aaa", "aaaa", ...
  59. */
  60. static uint hash(const uchar *p, int n)
  61. {
  62. uint h = 0;
  63. while (n--) {
  64. h = (h << 4) + *p++;
  65. h ^= (h & 0xf0000000) >> 23;
  66. h &= 0x0fffffff;
  67. }
  68. return h;
  69. }
  70. static uint hash(const QChar *p, int n)
  71. {
  72. uint h = 0;
  73. while (n--) {
  74. h = (h << 4) + (*p++).unicode();
  75. h ^= (h & 0xf0000000) >> 23;
  76. h &= 0x0fffffff;
  77. }
  78. return h;
  79. }
  80. uint qHash(const QByteArray &key)
  81. {
  82. return hash(reinterpret_cast<const uchar *>(key.constData()), key.size());
  83. }
  84. uint qHash(const QString &key)
  85. {
  86. return hash(key.unicode(), key.size());
  87. }
  88. uint qHash(const QStringRef &key)
  89. {
  90. return hash(key.unicode(), key.size());
  91. }
  92. uint qHash(const QBitArray &bitArray)
  93. {
  94. int m = bitArray.d.size() - 1;
  95. uint result = hash(reinterpret_cast<const uchar *>(bitArray.d.constData()), qMax(0, m));
  96. // deal with the last 0 to 7 bits manually, because we can't trust that
  97. // the padding is initialized to 0 in bitArray.d
  98. int n = bitArray.size();
  99. if (n & 0x7)
  100. result = ((result << 4) + bitArray.d.at(m)) & ((1 << n) - 1);
  101. return result;
  102. }
  103. /*
  104. The prime_deltas array is a table of selected prime values, even
  105. though it doesn't look like one. The primes we are using are 1,
  106. 2, 5, 11, 17, 37, 67, 131, 257, ..., i.e. primes in the immediate
  107. surrounding of a power of two.
  108. The primeForNumBits() function returns the prime associated to a
  109. power of two. For example, primeForNumBits(8) returns 257.
  110. */
  111. static const uchar prime_deltas[] = {
  112. 0, 0, 1, 3, 1, 5, 3, 3, 1, 9, 7, 5, 3, 9, 25, 3,
  113. 1, 21, 3, 21, 7, 15, 9, 5, 3, 29, 15, 0, 0, 0, 0, 0
  114. };
  115. static inline int primeForNumBits(int numBits)
  116. {
  117. return (1 << numBits) + prime_deltas[numBits];
  118. }
  119. /*
  120. Returns the smallest integer n such that
  121. primeForNumBits(n) >= hint.
  122. */
  123. static int countBits(int hint)
  124. {
  125. int numBits = 0;
  126. int bits = hint;
  127. while (bits > 1) {
  128. bits >>= 1;
  129. numBits++;
  130. }
  131. if (numBits >= (int)sizeof(prime_deltas)) {
  132. numBits = sizeof(prime_deltas) - 1;
  133. } else if (primeForNumBits(numBits) < hint) {
  134. ++numBits;
  135. }
  136. return numBits;
  137. }
  138. /*
  139. A QHash has initially around pow(2, MinNumBits) buckets. For
  140. example, if MinNumBits is 4, it has 17 buckets.
  141. */
  142. const int MinNumBits = 4;
  143. QHashData QHashData::shared_null = {
  144. 0, 0, Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, MinNumBits, 0, 0, true, false, 0
  145. };
  146. void *QHashData::allocateNode()
  147. {
  148. return allocateNode(0);
  149. }
  150. void *QHashData::allocateNode(int nodeAlign)
  151. {
  152. void *ptr = strictAlignment ? qMallocAligned(nodeSize, nodeAlign) : qMalloc(nodeSize);
  153. Q_CHECK_PTR(ptr);
  154. return ptr;
  155. }
  156. void QHashData::freeNode(void *node)
  157. {
  158. if (strictAlignment)
  159. qFreeAligned(node);
  160. else
  161. qFree(node);
  162. }
  163. QHashData *QHashData::detach_helper(void (*node_duplicate)(Node *, void *), int nodeSize)
  164. {
  165. return detach_helper2( node_duplicate, 0, nodeSize, 0 );
  166. }
  167. QHashData *QHashData::detach_helper2(void (*node_duplicate)(Node *, void *),
  168. void (*node_delete)(Node *),
  169. int nodeSize,
  170. int nodeAlign)
  171. {
  172. union {
  173. QHashData *d;
  174. Node *e;
  175. };
  176. d = new QHashData;
  177. d->fakeNext = 0;
  178. d->buckets = 0;
  179. d->ref = 1;
  180. d->size = size;
  181. d->nodeSize = nodeSize;
  182. d->userNumBits = userNumBits;
  183. d->numBits = numBits;
  184. d->numBuckets = numBuckets;
  185. d->sharable = true;
  186. d->strictAlignment = nodeAlign > 8;
  187. d->reserved = 0;
  188. if (numBuckets) {
  189. QT_TRY {
  190. d->buckets = new Node *[numBuckets];
  191. } QT_CATCH(...) {
  192. // restore a consistent state for d
  193. d->numBuckets = 0;
  194. // roll back
  195. d->free_helper(node_delete);
  196. QT_RETHROW;
  197. }
  198. Node *this_e = reinterpret_cast<Node *>(this);
  199. for (int i = 0; i < numBuckets; ++i) {
  200. Node **nextNode = &d->buckets[i];
  201. Node *oldNode = buckets[i];
  202. while (oldNode != this_e) {
  203. QT_TRY {
  204. Node *dup = static_cast<Node *>(allocateNode(nodeAlign));
  205. QT_TRY {
  206. node_duplicate(oldNode, dup);
  207. } QT_CATCH(...) {
  208. freeNode( dup );
  209. QT_RETHROW;
  210. }
  211. dup->h = oldNode->h;
  212. *nextNode = dup;
  213. nextNode = &dup->next;
  214. oldNode = oldNode->next;
  215. } QT_CATCH(...) {
  216. // restore a consistent state for d
  217. *nextNode = e;
  218. d->numBuckets = i+1;
  219. // roll back
  220. d->free_helper(node_delete);
  221. QT_RETHROW;
  222. }
  223. }
  224. *nextNode = e;
  225. }
  226. }
  227. return d;
  228. }
  229. void QHashData::free_helper(void (*node_delete)(Node *))
  230. {
  231. if (node_delete) {
  232. Node *this_e = reinterpret_cast<Node *>(this);
  233. Node **bucket = reinterpret_cast<Node **>(this->buckets);
  234. int n = numBuckets;
  235. while (n--) {
  236. Node *cur = *bucket++;
  237. while (cur != this_e) {
  238. Node *next = cur->next;
  239. node_delete(cur);
  240. freeNode(cur);
  241. cur = next;
  242. }
  243. }
  244. }
  245. delete [] buckets;
  246. delete this;
  247. }
  248. QHashData::Node *QHashData::nextNode(Node *node)
  249. {
  250. union {
  251. Node *next;
  252. Node *e;
  253. QHashData *d;
  254. };
  255. next = node->next;
  256. Q_ASSERT_X(next, "QHash", "Iterating beyond end()");
  257. if (next->next)
  258. return next;
  259. int start = (node->h % d->numBuckets) + 1;
  260. Node **bucket = d->buckets + start;
  261. int n = d->numBuckets - start;
  262. while (n--) {
  263. if (*bucket != e)
  264. return *bucket;
  265. ++bucket;
  266. }
  267. return e;
  268. }
  269. QHashData::Node *QHashData::previousNode(Node *node)
  270. {
  271. union {
  272. Node *e;
  273. QHashData *d;
  274. };
  275. e = node;
  276. while (e->next)
  277. e = e->next;
  278. int start;
  279. if (node == e)
  280. start = d->numBuckets - 1;
  281. else
  282. start = node->h % d->numBuckets;
  283. Node *sentinel = node;
  284. Node **bucket = d->buckets + start;
  285. while (start >= 0) {
  286. if (*bucket != sentinel) {
  287. Node *prev = *bucket;
  288. while (prev->next != sentinel)
  289. prev = prev->next;
  290. return prev;
  291. }
  292. sentinel = e;
  293. --bucket;
  294. --start;
  295. }
  296. Q_ASSERT_X(start >= 0, "QHash", "Iterating backward beyond begin()");
  297. return e;
  298. }
  299. /*
  300. If hint is negative, -hint gives the approximate number of
  301. buckets that should be used for the hash table. If hint is
  302. nonnegative, (1 << hint) gives the approximate number
  303. of buckets that should be used.
  304. */
  305. void QHashData::rehash(int hint)
  306. {
  307. if (hint < 0) {
  308. hint = countBits(-hint);
  309. if (hint < MinNumBits)
  310. hint = MinNumBits;
  311. userNumBits = hint;
  312. while (primeForNumBits(hint) < (size >> 1))
  313. ++hint;
  314. } else if (hint < MinNumBits) {
  315. hint = MinNumBits;
  316. }
  317. if (numBits != hint) {
  318. Node *e = reinterpret_cast<Node *>(this);
  319. Node **oldBuckets = buckets;
  320. int oldNumBuckets = numBuckets;
  321. int nb = primeForNumBits(hint);
  322. buckets = new Node *[nb];
  323. numBits = hint;
  324. numBuckets = nb;
  325. for (int i = 0; i < numBuckets; ++i)
  326. buckets[i] = e;
  327. for (int i = 0; i < oldNumBuckets; ++i) {
  328. Node *firstNode = oldBuckets[i];
  329. while (firstNode != e) {
  330. uint h = firstNode->h;
  331. Node *lastNode = firstNode;
  332. while (lastNode->next != e && lastNode->next->h == h)
  333. lastNode = lastNode->next;
  334. Node *afterLastNode = lastNode->next;
  335. Node **beforeFirstNode = &buckets[h % numBuckets];
  336. while (*beforeFirstNode != e)
  337. beforeFirstNode = &(*beforeFirstNode)->next;
  338. lastNode->next = *beforeFirstNode;
  339. *beforeFirstNode = firstNode;
  340. firstNode = afterLastNode;
  341. }
  342. }
  343. delete [] oldBuckets;
  344. }
  345. }
  346. void QHashData::destroyAndFree()
  347. {
  348. free_helper(0);
  349. }
  350. #ifdef QT_QHASH_DEBUG
  351. void QHashData::dump()
  352. {
  353. qDebug("Hash data (ref = %d, size = %d, nodeSize = %d, userNumBits = %d, numBits = %d, numBuckets = %d)",
  354. int(ref), size, nodeSize, userNumBits, numBits,
  355. numBuckets);
  356. qDebug(" %p (fakeNode = %p)", this, fakeNext);
  357. for (int i = 0; i < numBuckets; ++i) {
  358. QString line;
  359. Node *n = buckets[i];
  360. if (n != reinterpret_cast<Node *>(this)) {
  361. line.sprintf("%d:", i);
  362. while (n != reinterpret_cast<Node *>(this)) {
  363. line += QString().sprintf(" -> [%p]", n);
  364. if (!n) {
  365. line += " (CORRUPT)";
  366. break;
  367. }
  368. n = n->next;
  369. }
  370. qDebug(qPrintable(line));
  371. }
  372. }
  373. }
  374. void QHashData::checkSanity()
  375. {
  376. if (fakeNext)
  377. qFatal("Fake next isn't 0");
  378. for (int i = 0; i < numBuckets; ++i) {
  379. Node *n = buckets[i];
  380. Node *p = n;
  381. if (!n)
  382. qFatal("%d: Bucket entry is 0", i);
  383. if (n != reinterpret_cast<Node *>(this)) {
  384. while (n != reinterpret_cast<Node *>(this)) {
  385. if (!n->next)
  386. qFatal("%d: Next of %p is 0, should be %p", i, n, this);
  387. n = n->next;
  388. }
  389. }
  390. }
  391. }
  392. #endif
  393. /*!
  394. \fn uint qHash(const QPair<T1, T2> &key)
  395. \since 4.3
  396. \relates QHash
  397. Returns the hash value for the \a key.
  398. Types \c T1 and \c T2 must be supported by qHash().
  399. */
  400. /*! \fn uint qHash(char key)
  401. \relates QHash
  402. Returns the hash value for the \a key.
  403. */
  404. /*! \fn uint qHash(uchar key)
  405. \relates QHash
  406. Returns the hash value for the \a key.
  407. */
  408. /*! \fn uint qHash(signed char key)
  409. \relates QHash
  410. Returns the hash value for the \a key.
  411. */
  412. /*! \fn uint qHash(ushort key)
  413. \relates QHash
  414. Returns the hash value for the \a key.
  415. */
  416. /*! \fn uint qHash(short key)
  417. \relates QHash
  418. Returns the hash value for the \a key.
  419. */
  420. /*! \fn uint qHash(uint key)
  421. \relates QHash
  422. Returns the hash value for the \a key.
  423. */
  424. /*! \fn uint qHash(int key)
  425. \relates QHash
  426. Returns the hash value for the \a key.
  427. */
  428. /*! \fn uint qHash(ulong key)
  429. \relates QHash
  430. Returns the hash value for the \a key.
  431. */
  432. /*! \fn uint qHash(long key)
  433. \relates QHash
  434. Returns the hash value for the \a key.
  435. */
  436. /*! \fn uint qHash(quint64 key)
  437. \relates QHash
  438. Returns the hash value for the \a key.
  439. */
  440. /*! \fn uint qHash(qint64 key)
  441. \relates QHash
  442. Returns the hash value for the \a key.
  443. */
  444. /*! \fn uint qHash(QChar key)
  445. \relates QHash
  446. Returns the hash value for the \a key.
  447. */
  448. /*! \fn uint qHash(const QByteArray &key)
  449. \fn uint qHash(const QBitArray &key)
  450. \relates QHash
  451. Returns the hash value for the \a key.
  452. */
  453. /*! \fn uint qHash(const QString &key)
  454. \relates QHash
  455. Returns the hash value for the \a key.
  456. */
  457. /*! \fn uint qHash(const T *key)
  458. \relates QHash
  459. Returns the hash value for the \a key.
  460. */
  461. /*!
  462. \class QHash
  463. \brief The QHash class is a template class that provides a hash-table-based dictionary.
  464. \ingroup tools
  465. \ingroup shared
  466. \reentrant
  467. QHash\<Key, T\> is one of Qt's generic \l{container classes}. It
  468. stores (key, value) pairs and provides very fast lookup of the
  469. value associated with a key.
  470. QHash provides very similar functionality to QMap. The
  471. differences are:
  472. \list
  473. \i QHash provides faster lookups than QMap. (See \l{Algorithmic
  474. Complexity} for details.)
  475. \i When iterating over a QMap, the items are always sorted by
  476. key. With QHash, the items are arbitrarily ordered.
  477. \i The key type of a QMap must provide operator<(). The key
  478. type of a QHash must provide operator==() and a global
  479. hash function called qHash() (see the related non-member
  480. functions).
  481. \endlist
  482. Here's an example QHash with QString keys and \c int values:
  483. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 0
  484. To insert a (key, value) pair into the hash, you can use operator[]():
  485. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 1
  486. This inserts the following three (key, value) pairs into the
  487. QHash: ("one", 1), ("three", 3), and ("seven", 7). Another way to
  488. insert items into the hash is to use insert():
  489. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 2
  490. To look up a value, use operator[]() or value():
  491. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 3
  492. If there is no item with the specified key in the hash, these
  493. functions return a \l{default-constructed value}.
  494. If you want to check whether the hash contains a particular key,
  495. use contains():
  496. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 4
  497. There is also a value() overload that uses its second argument as
  498. a default value if there is no item with the specified key:
  499. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 5
  500. In general, we recommend that you use contains() and value()
  501. rather than operator[]() for looking up a key in a hash. The
  502. reason is that operator[]() silently inserts an item into the
  503. hash if no item exists with the same key (unless the hash is
  504. const). For example, the following code snippet will create 1000
  505. items in memory:
  506. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 6
  507. To avoid this problem, replace \c hash[i] with \c hash.value(i)
  508. in the code above.
  509. If you want to navigate through all the (key, value) pairs stored
  510. in a QHash, you can use an iterator. QHash provides both
  511. \l{Java-style iterators} (QHashIterator and QMutableHashIterator)
  512. and \l{STL-style iterators} (QHash::const_iterator and
  513. QHash::iterator). Here's how to iterate over a QHash<QString,
  514. int> using a Java-style iterator:
  515. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 7
  516. Here's the same code, but using an STL-style iterator:
  517. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 8
  518. QHash is unordered, so an iterator's sequence cannot be assumed
  519. to be predictable. If ordering by key is required, use a QMap.
  520. Normally, a QHash allows only one value per key. If you call
  521. insert() with a key that already exists in the QHash, the
  522. previous value is erased. For example:
  523. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 9
  524. However, you can store multiple values per key by using
  525. insertMulti() instead of insert() (or using the convenience
  526. subclass QMultiHash). If you want to retrieve all
  527. the values for a single key, you can use values(const Key &key),
  528. which returns a QList<T>:
  529. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 10
  530. The items that share the same key are available from most
  531. recently to least recently inserted. A more efficient approach is
  532. to call find() to get the iterator for the first item with a key
  533. and iterate from there:
  534. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 11
  535. If you only need to extract the values from a hash (not the keys),
  536. you can also use \l{foreach}:
  537. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 12
  538. Items can be removed from the hash in several ways. One way is to
  539. call remove(); this will remove any item with the given key.
  540. Another way is to use QMutableHashIterator::remove(). In addition,
  541. you can clear the entire hash using clear().
  542. QHash's key and value data types must be \l{assignable data
  543. types}. You cannot, for example, store a QWidget as a value;
  544. instead, store a QWidget *. In addition, QHash's key type must
  545. provide operator==(), and there must also be a global qHash()
  546. function that returns a hash value for an argument of the key's
  547. type.
  548. Here's a list of the C++ and Qt types that can serve as keys in a
  549. QHash: any integer type (char, unsigned long, etc.), any pointer
  550. type, QChar, QString, and QByteArray. For all of these, the \c
  551. <QHash> header defines a qHash() function that computes an
  552. adequate hash value. If you want to use other types as the key,
  553. make sure that you provide operator==() and a qHash()
  554. implementation.
  555. Example:
  556. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 13
  557. The qHash() function computes a numeric value based on a key. It
  558. can use any algorithm imaginable, as long as it always returns
  559. the same value if given the same argument. In other words, if
  560. \c{e1 == e2}, then \c{qHash(e1) == qHash(e2)} must hold as well.
  561. However, to obtain good performance, the qHash() function should
  562. attempt to return different hash values for different keys to the
  563. largest extent possible.
  564. In the example above, we've relied on Qt's global qHash(const
  565. QString &) to give us a hash value for the employee's name, and
  566. XOR'ed this with the day they were born to help produce unique
  567. hashes for people with the same name.
  568. Internally, QHash uses a hash table to perform lookups. Unlike Qt
  569. 3's \c QDict class, which needed to be initialized with a prime
  570. number, QHash's hash table automatically grows and shrinks to
  571. provide fast lookups without wasting too much memory. You can
  572. still control the size of the hash table by calling reserve() if
  573. you already know approximately how many items the QHash will
  574. contain, but this isn't necessary to obtain good performance. You
  575. can also call capacity() to retrieve the hash table's size.
  576. \sa QHashIterator, QMutableHashIterator, QMap, QSet
  577. */
  578. /*! \fn QHash::QHash()
  579. Constructs an empty hash.
  580. \sa clear()
  581. */
  582. /*! \fn QHash::QHash(const QHash<Key, T> &other)
  583. Constructs a copy of \a other.
  584. This operation occurs in \l{constant time}, because QHash is
  585. \l{implicitly shared}. This makes returning a QHash from a
  586. function very fast. If a shared instance is modified, it will be
  587. copied (copy-on-write), and this takes \l{linear time}.
  588. \sa operator=()
  589. */
  590. /*! \fn QHash::~QHash()
  591. Destroys the hash. References to the values in the hash and all
  592. iterators of this hash become invalid.
  593. */
  594. /*! \fn QHash<Key, T> &QHash::operator=(const QHash<Key, T> &other)
  595. Assigns \a other to this hash and returns a reference to this hash.
  596. */
  597. /*! \fn void QHash::swap(QHash<Key, T> &other)
  598. \since 4.8
  599. Swaps hash \a other with this hash. This operation is very
  600. fast and never fails.
  601. */
  602. /*! \fn void QMultiHash::swap(QMultiHash<Key, T> &other)
  603. \since 4.8
  604. Swaps hash \a other with this hash. This operation is very
  605. fast and never fails.
  606. */
  607. /*! \fn bool QHash::operator==(const QHash<Key, T> &other) const
  608. Returns true if \a other is equal to this hash; otherwise returns
  609. false.
  610. Two hashes are considered equal if they contain the same (key,
  611. value) pairs.
  612. This function requires the value type to implement \c operator==().
  613. \sa operator!=()
  614. */
  615. /*! \fn bool QHash::operator!=(const QHash<Key, T> &other) const
  616. Returns true if \a other is not equal to this hash; otherwise
  617. returns false.
  618. Two hashes are considered equal if they contain the same (key,
  619. value) pairs.
  620. This function requires the value type to implement \c operator==().
  621. \sa operator==()
  622. */
  623. /*! \fn int QHash::size() const
  624. Returns the number of items in the hash.
  625. \sa isEmpty(), count()
  626. */
  627. /*! \fn bool QHash::isEmpty() const
  628. Returns true if the hash contains no items; otherwise returns
  629. false.
  630. \sa size()
  631. */
  632. /*! \fn int QHash::capacity() const
  633. Returns the number of buckets in the QHash's internal hash table.
  634. The sole purpose of this function is to provide a means of fine
  635. tuning QHash's memory usage. In general, you will rarely ever
  636. need to call this function. If you want to know how many items are
  637. in the hash, call size().
  638. \sa reserve(), squeeze()
  639. */
  640. /*! \fn void QHash::reserve(int size)
  641. Ensures that the QHash's internal hash table consists of at least
  642. \a size buckets.
  643. This function is useful for code that needs to build a huge hash
  644. and wants to avoid repeated reallocation. For example:
  645. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 14
  646. Ideally, \a size should be slightly more than the maximum number
  647. of items expected in the hash. \a size doesn't have to be prime,
  648. because QHash will use a prime number internally anyway. If \a size
  649. is an underestimate, the worst that will happen is that the QHash
  650. will be a bit slower.
  651. In general, you will rarely ever need to call this function.
  652. QHash's internal hash table automatically shrinks or grows to
  653. provide good performance without wasting too much memory.
  654. \sa squeeze(), capacity()
  655. */
  656. /*! \fn void QHash::squeeze()
  657. Reduces the size of the QHash's internal hash table to save
  658. memory.
  659. The sole purpose of this function is to provide a means of fine
  660. tuning QHash's memory usage. In general, you will rarely ever
  661. need to call this function.
  662. \sa reserve(), capacity()
  663. */
  664. /*! \fn void QHash::detach()
  665. \internal
  666. Detaches this hash from any other hashes with which it may share
  667. data.
  668. \sa isDetached()
  669. */
  670. /*! \fn bool QHash::isDetached() const
  671. \internal
  672. Returns true if the hash's internal data isn't shared with any
  673. other hash object; otherwise returns false.
  674. \sa detach()
  675. */
  676. /*! \fn void QHash::setSharable(bool sharable)
  677. \internal
  678. */
  679. /*! \fn bool QHash::isSharedWith(const QHash<Key, T> &other) const
  680. \internal
  681. */
  682. /*! \fn void QHash::clear()
  683. Removes all items from the hash.
  684. \sa remove()
  685. */
  686. /*! \fn int QHash::remove(const Key &key)
  687. Removes all the items that have the \a key from the hash.
  688. Returns the number of items removed which is usually 1 but will
  689. be 0 if the key isn't in the hash, or greater than 1 if
  690. insertMulti() has been used with the \a key.
  691. \sa clear(), take(), QMultiHash::remove()
  692. */
  693. /*! \fn T QHash::take(const Key &key)
  694. Removes the item with the \a key from the hash and returns
  695. the value associated with it.
  696. If the item does not exist in the hash, the function simply
  697. returns a \l{default-constructed value}. If there are multiple
  698. items for \a key in the hash, only the most recently inserted one
  699. is removed.
  700. If you don't use the return value, remove() is more efficient.
  701. \sa remove()
  702. */
  703. /*! \fn bool QHash::contains(const Key &key) const
  704. Returns true if the hash contains an item with the \a key;
  705. otherwise returns false.
  706. \sa count(), QMultiHash::contains()
  707. */
  708. /*! \fn const T QHash::value(const Key &key) const
  709. Returns the value associated with the \a key.
  710. If the hash contains no item with the \a key, the function
  711. returns a \l{default-constructed value}. If there are multiple
  712. items for the \a key in the hash, the value of the most recently
  713. inserted one is returned.
  714. \sa key(), values(), contains(), operator[]()
  715. */
  716. /*! \fn const T QHash::value(const Key &key, const T &defaultValue) const
  717. \overload
  718. If the hash contains no item with the given \a key, the function returns
  719. \a defaultValue.
  720. */
  721. /*! \fn T &QHash::operator[](const Key &key)
  722. Returns the value associated with the \a key as a modifiable
  723. reference.
  724. If the hash contains no item with the \a key, the function inserts
  725. a \l{default-constructed value} into the hash with the \a key, and
  726. returns a reference to it. If the hash contains multiple items
  727. with the \a key, this function returns a reference to the most
  728. recently inserted value.
  729. \sa insert(), value()
  730. */
  731. /*! \fn const T QHash::operator[](const Key &key) const
  732. \overload
  733. Same as value().
  734. */
  735. /*! \fn QList<Key> QHash::uniqueKeys() const
  736. \since 4.2
  737. Returns a list containing all the keys in the map. Keys that occur multiple
  738. times in the map (because items were inserted with insertMulti(), or
  739. unite() was used) occur only once in the returned list.
  740. \sa keys(), values()
  741. */
  742. /*! \fn QList<Key> QHash::keys() const
  743. Returns a list containing all the keys in the hash, in an
  744. arbitrary order. Keys that occur multiple times in the hash
  745. (because items were inserted with insertMulti(), or unite() was
  746. used) also occur multiple times in the list.
  747. To obtain a list of unique keys, where each key from the map only
  748. occurs once, use uniqueKeys().
  749. The order is guaranteed to be the same as that used by values().
  750. \sa uniqueKeys(), values(), key()
  751. */
  752. /*! \fn QList<Key> QHash::keys(const T &value) const
  753. \overload
  754. Returns a list containing all the keys associated with value \a
  755. value, in an arbitrary order.
  756. This function can be slow (\l{linear time}), because QHash's
  757. internal data structure is optimized for fast lookup by key, not
  758. by value.
  759. */
  760. /*! \fn QList<T> QHash::values() const
  761. Returns a list containing all the values in the hash, in an
  762. arbitrary order. If a key is associated multiple values, all of
  763. its values will be in the list, and not just the most recently
  764. inserted one.
  765. The order is guaranteed to be the same as that used by keys().
  766. \sa keys(), value()
  767. */
  768. /*! \fn QList<T> QHash::values(const Key &key) const
  769. \overload
  770. Returns a list of all the values associated with the \a key,
  771. from the most recently inserted to the least recently inserted.
  772. \sa count(), insertMulti()
  773. */
  774. /*! \fn Key QHash::key(const T &value) const
  775. Returns the first key mapped to \a value.
  776. If the hash contains no item with the \a value, the function
  777. returns a \link {default-constructed value} default-constructed
  778. key \endlink.
  779. This function can be slow (\l{linear time}), because QHash's
  780. internal data structure is optimized for fast lookup by key, not
  781. by value.
  782. \sa value(), keys()
  783. */
  784. /*!
  785. \fn Key QHash::key(const T &value, const Key &defaultKey) const
  786. \since 4.3
  787. \overload
  788. Returns the first key mapped to \a value, or \a defaultKey if the
  789. hash contains no item mapped to \a value.
  790. This function can be slow (\l{linear time}), because QHash's
  791. internal data structure is optimized for fast lookup by key, not
  792. by value.
  793. */
  794. /*! \fn int QHash::count(const Key &key) const
  795. Returns the number of items associated with the \a key.
  796. \sa contains(), insertMulti()
  797. */
  798. /*! \fn int QHash::count() const
  799. \overload
  800. Same as size().
  801. */
  802. /*! \fn QHash::iterator QHash::begin()
  803. Returns an \l{STL-style iterator} pointing to the first item in
  804. the hash.
  805. \sa constBegin(), end()
  806. */
  807. /*! \fn QHash::const_iterator QHash::begin() const
  808. \overload
  809. */
  810. /*! \fn QHash::const_iterator QHash::constBegin() const
  811. Returns a const \l{STL-style iterator} pointing to the first item
  812. in the hash.
  813. \sa begin(), constEnd()
  814. */
  815. /*! \fn QHash::iterator QHash::end()
  816. Returns an \l{STL-style iterator} pointing to the imaginary item
  817. after the last item in the hash.
  818. \sa begin(), constEnd()
  819. */
  820. /*! \fn QHash::const_iterator QHash::end() const
  821. \overload
  822. */
  823. /*! \fn QHash::const_iterator QHash::constEnd() const
  824. Returns a const \l{STL-style iterator} pointing to the imaginary
  825. item after the last item in the hash.
  826. \sa constBegin(), end()
  827. */
  828. /*! \fn QHash::iterator QHash::erase(iterator pos)
  829. Removes the (key, value) pair associated with the iterator \a pos
  830. from the hash, and returns an iterator to the next item in the
  831. hash.
  832. Unlike remove() and take(), this function never causes QHash to
  833. rehash its internal data structure. This means that it can safely
  834. be called while iterating, and won't affect the order of items in
  835. the hash. For example:
  836. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 15
  837. \sa remove(), take(), find()
  838. */
  839. /*! \fn QHash::iterator QHash::find(const Key &key)
  840. Returns an iterator pointing to the item with the \a key in the
  841. hash.
  842. If the hash contains no item with the \a key, the function
  843. returns end().
  844. If the hash contains multiple items with the \a key, this
  845. function returns an iterator that points to the most recently
  846. inserted value. The other values are accessible by incrementing
  847. the iterator. For example, here's some code that iterates over all
  848. the items with the same key:
  849. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 16
  850. \sa value(), values(), QMultiHash::find()
  851. */
  852. /*! \fn QHash::const_iterator QHash::find(const Key &key) const
  853. \overload
  854. */
  855. /*! \fn QHash::iterator QHash::constFind(const Key &key) const
  856. \since 4.1
  857. Returns an iterator pointing to the item with the \a key in the
  858. hash.
  859. If the hash contains no item with the \a key, the function
  860. returns constEnd().
  861. \sa find(), QMultiHash::constFind()
  862. */
  863. /*! \fn QHash::iterator QHash::insert(const Key &key, const T &value)
  864. Inserts a new item with the \a key and a value of \a value.
  865. If there is already an item with the \a key, that item's value
  866. is replaced with \a value.
  867. If there are multiple items with the \a key, the most
  868. recently inserted item's value is replaced with \a value.
  869. \sa insertMulti()
  870. */
  871. /*! \fn QHash::iterator QHash::insertMulti(const Key &key, const T &value)
  872. Inserts a new item with the \a key and a value of \a value.
  873. If there is already an item with the same key in the hash, this
  874. function will simply create a new one. (This behavior is
  875. different from insert(), which overwrites the value of an
  876. existing item.)
  877. \sa insert(), values()
  878. */
  879. /*! \fn QHash<Key, T> &QHash::unite(const QHash<Key, T> &other)
  880. Inserts all the items in the \a other hash into this hash. If a
  881. key is common to both hashes, the resulting hash will contain the
  882. key multiple times.
  883. \sa insertMulti()
  884. */
  885. /*! \fn bool QHash::empty() const
  886. This function is provided for STL compatibility. It is equivalent
  887. to isEmpty(), returning true if the hash is empty; otherwise
  888. returns false.
  889. */
  890. /*! \typedef QHash::ConstIterator
  891. Qt-style synonym for QHash::const_iterator.
  892. */
  893. /*! \typedef QHash::Iterator
  894. Qt-style synonym for QHash::iterator.
  895. */
  896. /*! \typedef QHash::difference_type
  897. Typedef for ptrdiff_t. Provided for STL compatibility.
  898. */
  899. /*! \typedef QHash::key_type
  900. Typedef for Key. Provided for STL compatibility.
  901. */
  902. /*! \typedef QHash::mapped_type
  903. Typedef for T. Provided for STL compatibility.
  904. */
  905. /*! \typedef QHash::size_type
  906. Typedef for int. Provided for STL compatibility.
  907. */
  908. /*! \typedef QHash::iterator::difference_type
  909. \internal
  910. */
  911. /*! \typedef QHash::iterator::iterator_category
  912. \internal
  913. */
  914. /*! \typedef QHash::iterator::pointer
  915. \internal
  916. */
  917. /*! \typedef QHash::iterator::reference
  918. \internal
  919. */
  920. /*! \typedef QHash::iterator::value_type
  921. \internal
  922. */
  923. /*! \typedef QHash::const_iterator::difference_type
  924. \internal
  925. */
  926. /*! \typedef QHash::const_iterator::iterator_category
  927. \internal
  928. */
  929. /*! \typedef QHash::const_iterator::pointer
  930. \internal
  931. */
  932. /*! \typedef QHash::const_iterator::reference
  933. \internal
  934. */
  935. /*! \typedef QHash::const_iterator::value_type
  936. \internal
  937. */
  938. /*! \class QHash::iterator
  939. \brief The QHash::iterator class provides an STL-style non-const iterator for QHash and QMultiHash.
  940. QHash features both \l{STL-style iterators} and \l{Java-style
  941. iterators}. The STL-style iterators are more low-level and more
  942. cumbersome to use; on the other hand, they are slightly faster
  943. and, for developers who already know STL, have the advantage of
  944. familiarity.
  945. QHash\<Key, T\>::iterator allows you to iterate over a QHash (or
  946. QMultiHash) and to modify the value (but not the key) associated
  947. with a particular key. If you want to iterate over a const QHash,
  948. you should use QHash::const_iterator. It is generally good
  949. practice to use QHash::const_iterator on a non-const QHash as
  950. well, unless you need to change the QHash through the iterator.
  951. Const iterators are slightly faster, and can improve code
  952. readability.
  953. The default QHash::iterator constructor creates an uninitialized
  954. iterator. You must initialize it using a QHash function like
  955. QHash::begin(), QHash::end(), or QHash::find() before you can
  956. start iterating. Here's a typical loop that prints all the (key,
  957. value) pairs stored in a hash:
  958. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 17
  959. Unlike QMap, which orders its items by key, QHash stores its
  960. items in an arbitrary order. The only guarantee is that items that
  961. share the same key (because they were inserted using
  962. QHash::insertMulti()) will appear consecutively, from the most
  963. recently to the least recently inserted value.
  964. Let's see a few examples of things we can do with a
  965. QHash::iterator that we cannot do with a QHash::const_iterator.
  966. Here's an example that increments every value stored in the QHash
  967. by 2:
  968. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 18
  969. Here's an example that removes all the items whose key is a
  970. string that starts with an underscore character:
  971. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 19
  972. The call to QHash::erase() removes the item pointed to by the
  973. iterator from the hash, and returns an iterator to the next item.
  974. Here's another way of removing an item while iterating:
  975. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 20
  976. It might be tempting to write code like this:
  977. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 21
  978. However, this will potentially crash in \c{++i}, because \c i is
  979. a dangling iterator after the call to erase().
  980. Multiple iterators can be used on the same hash. However, be
  981. aware that any modification performed directly on the QHash has
  982. the potential of dramatically changing the order in which the
  983. items are stored in the hash, as they might cause QHash to rehash
  984. its internal data structure. There is one notable exception:
  985. QHash::erase(). This function can safely be called while
  986. iterating, and won't affect the order of items in the hash. If you
  987. need to keep iterators over a long period of time, we recommend
  988. that you use QMap rather than QHash.
  989. \sa QHash::const_iterator, QMutableHashIterator
  990. */
  991. /*! \fn QHash::iterator::operator Node *() const
  992. \internal
  993. */
  994. /*! \fn QHash::iterator::iterator()
  995. Constructs an uninitialized iterator.
  996. Functions like key(), value(), and operator++() must not be
  997. called on an uninitialized iterator. Use operator=() to assign a
  998. value to it before using it.
  999. \sa QHash::begin() QHash::end()
  1000. */
  1001. /*! \fn QHash::iterator::iterator(void *node)
  1002. \internal
  1003. */
  1004. /*! \fn const Key &QHash::iterator::key() const
  1005. Returns the current item's key as a const reference.
  1006. There is no direct way of changing an item's key through an
  1007. iterator, although it can be done by calling QHash::erase()
  1008. followed by QHash::insert() or QHash::insertMulti().
  1009. \sa value()
  1010. */
  1011. /*! \fn T &QHash::iterator::value() const
  1012. Returns a modifiable reference to the current item's value.
  1013. You can change the value of an item by using value() on
  1014. the left side of an assignment, for example:
  1015. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 22
  1016. \sa key(), operator*()
  1017. */
  1018. /*! \fn T &QHash::iterator::operator*() const
  1019. Returns a modifiable reference to the current item's value.
  1020. Same as value().
  1021. \sa key()
  1022. */
  1023. /*! \fn T *QHash::iterator::operator->() const
  1024. Returns a pointer to the current item's value.
  1025. \sa value()
  1026. */
  1027. /*!
  1028. \fn bool QHash::iterator::operator==(const iterator &other) const
  1029. \fn bool QHash::iterator::operator==(const const_iterator &other) const
  1030. Returns true if \a other points to the same item as this
  1031. iterator; otherwise returns false.
  1032. \sa operator!=()
  1033. */
  1034. /*!
  1035. \fn bool QHash::iterator::operator!=(const iterator &other) const
  1036. \fn bool QHash::iterator::operator!=(const const_iterator &other) const
  1037. Returns true if \a other points to a different item than this
  1038. iterator; otherwise returns false.
  1039. \sa operator==()
  1040. */
  1041. /*!
  1042. \fn QHash::iterator &QHash::iterator::operator++()
  1043. The prefix ++ operator (\c{++i}) advances the iterator to the
  1044. next item in the hash and returns an iterator to the new current
  1045. item.
  1046. Calling this function on QHash::end() leads to undefined results.
  1047. \sa operator--()
  1048. */
  1049. /*! \fn QHash::iterator QHash::iterator::operator++(int)
  1050. \overload
  1051. The postfix ++ operator (\c{i++}) advances the iterator to the
  1052. next item in the hash and returns an iterator to the previously
  1053. current item.
  1054. */
  1055. /*!
  1056. \fn QHash::iterator &QHash::iterator::operator--()
  1057. The prefix -- operator (\c{--i}) makes the preceding item
  1058. current and returns an iterator pointing to the new current item.
  1059. Calling this function on QHash::begin() leads to undefined
  1060. results.
  1061. \sa operator++()
  1062. */
  1063. /*!
  1064. \fn QHash::iterator QHash::iterator::operator--(int)
  1065. \overload
  1066. The postfix -- operator (\c{i--}) makes the preceding item
  1067. current and returns an iterator pointing to the previously
  1068. current item.
  1069. */
  1070. /*! \fn QHash::iterator QHash::iterator::operator+(int j) const
  1071. Returns an iterator to the item at \a j positions forward from
  1072. this iterator. (If \a j is negative, the iterator goes backward.)
  1073. This operation can be slow for large \a j values.
  1074. \sa operator-()
  1075. */
  1076. /*! \fn QHash::iterator QHash::iterator::operator-(int j) const
  1077. Returns an iterator to the item at \a j positions backward from
  1078. this iterator. (If \a j is negative, the iterator goes forward.)
  1079. This operation can be slow for large \a j values.
  1080. \sa operator+()
  1081. */
  1082. /*! \fn QHash::iterator &QHash::iterator::operator+=(int j)
  1083. Advances the iterator by \a j items. (If \a j is negative, the
  1084. iterator goes backward.)
  1085. \sa operator-=(), operator+()
  1086. */
  1087. /*! \fn QHash::iterator &QHash::iterator::operator-=(int j)
  1088. Makes the iterator go back by \a j items. (If \a j is negative,
  1089. the iterator goes forward.)
  1090. \sa operator+=(), operator-()
  1091. */
  1092. /*! \class QHash::const_iterator
  1093. \brief The QHash::const_iterator class provides an STL-style const iterator for QHash and QMultiHash.
  1094. QHash features both \l{STL-style iterators} and \l{Java-style
  1095. iterators}. The STL-style iterators are more low-level and more
  1096. cumbersome to use; on the other hand, they are slightly faster
  1097. and, for developers who already know STL, have the advantage of
  1098. familiarity.
  1099. QHash\<Key, T\>::const_iterator allows you to iterate over a
  1100. QHash (or a QMultiHash). If you want to modify the QHash as you
  1101. iterate over it, you must use QHash::iterator instead. It is
  1102. generally good practice to use QHash::const_iterator on a
  1103. non-const QHash as well, unless you need to change the QHash
  1104. through the iterator. Const iterators are slightly faster, and
  1105. can improve code readability.
  1106. The default QHash::const_iterator constructor creates an
  1107. uninitialized iterator. You must initialize it using a QHash
  1108. function like QHash::constBegin(), QHash::constEnd(), or
  1109. QHash::find() before you can start iterating. Here's a typical
  1110. loop that prints all the (key, value) pairs stored in a hash:
  1111. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 23
  1112. Unlike QMap, which orders its items by key, QHash stores its
  1113. items in an arbitrary order. The only guarantee is that items that
  1114. share the same key (because they were inserted using
  1115. QHash::insertMulti()) will appear consecutively, from the most
  1116. recently to the least recently inserted value.
  1117. Multiple iterators can be used on the same hash. However, be aware
  1118. that any modification performed directly on the QHash has the
  1119. potential of dramatically changing the order in which the items
  1120. are stored in the hash, as they might cause QHash to rehash its
  1121. internal data structure. If you need to keep iterators over a long
  1122. period of time, we recommend that you use QMap rather than QHash.
  1123. \sa QHash::iterator, QHashIterator
  1124. */
  1125. /*! \fn QHash::const_iterator::operator Node *() const
  1126. \internal
  1127. */
  1128. /*! \fn QHash::const_iterator::const_iterator()
  1129. Constructs an uninitialized iterator.
  1130. Functions like key(), value(), and operator++() must not be
  1131. called on an uninitialized iterator. Use operator=() to assign a
  1132. value to it before using it.
  1133. \sa QHash::constBegin() QHash::constEnd()
  1134. */
  1135. /*! \fn QHash::const_iterator::const_iterator(void *node)
  1136. \internal
  1137. */
  1138. /*! \fn QHash::const_iterator::const_iterator(const iterator &other)
  1139. Constructs a copy of \a other.
  1140. */
  1141. /*! \fn const Key &QHash::const_iterator::key() const
  1142. Returns the current item's key.
  1143. \sa value()
  1144. */
  1145. /*! \fn const T &QHash::const_iterator::value() const
  1146. Returns the current item's value.
  1147. \sa key(), operator*()
  1148. */
  1149. /*! \fn const T &QHash::const_iterator::operator*() const
  1150. Returns the current item's value.
  1151. Same as value().
  1152. \sa key()
  1153. */
  1154. /*! \fn const T *QHash::const_iterator::operator->() const
  1155. Returns a pointer to the current item's value.
  1156. \sa value()
  1157. */
  1158. /*! \fn bool QHash::const_iterator::operator==(const const_iterator &other) const
  1159. Returns true if \a other points to the same item as this
  1160. iterator; otherwise returns false.
  1161. \sa operator!=()
  1162. */
  1163. /*! \fn bool QHash::const_iterator::operator!=(const const_iterator &other) const
  1164. Returns true if \a other points to a different item than this
  1165. iterator; otherwise returns false.
  1166. \sa operator==()
  1167. */
  1168. /*!
  1169. \fn QHash::const_iterator &QHash::const_iterator::operator++()
  1170. The prefix ++ operator (\c{++i}) advances the iterator to the
  1171. next item in the hash and returns an iterator to the new current
  1172. item.
  1173. Calling this function on QHash::end() leads to undefined results.
  1174. \sa operator--()
  1175. */
  1176. /*! \fn QHash::const_iterator QHash::const_iterator::operator++(int)
  1177. \overload
  1178. The postfix ++ operator (\c{i++}) advances the iterator to the
  1179. next item in the hash and returns an iterator to the previously
  1180. current item.
  1181. */
  1182. /*! \fn QHash::const_iterator &QHash::const_iterator::operator--()
  1183. The prefix -- operator (\c{--i}) makes the preceding item
  1184. current and returns an iterator pointing to the new current item.
  1185. Calling this function on QHash::begin() leads to undefined
  1186. results.
  1187. \sa operator++()
  1188. */
  1189. /*! \fn QHash::const_iterator QHash::const_iterator::operator--(int)
  1190. \overload
  1191. The postfix -- operator (\c{i--}) makes the preceding item
  1192. current and returns an iterator pointing to the previously
  1193. current item.
  1194. */
  1195. /*! \fn QHash::const_iterator QHash::const_iterator::operator+(int j) const
  1196. Returns an iterator to the item at \a j positions forward from
  1197. this iterator. (If \a j is negative, the iterator goes backward.)
  1198. This operation can be slow for large \a j values.
  1199. \sa operator-()
  1200. */
  1201. /*! \fn QHash::const_iterator QHash::const_iterator::operator-(int j) const
  1202. Returns an iterator to the item at \a j positions backward from
  1203. this iterator. (If \a j is negative, the iterator goes forward.)
  1204. This operation can be slow for large \a j values.
  1205. \sa operator+()
  1206. */
  1207. /*! \fn QHash::const_iterator &QHash::const_iterator::operator+=(int j)
  1208. Advances the iterator by \a j items. (If \a j is negative, the
  1209. iterator goes backward.)
  1210. This operation can be slow for large \a j values.
  1211. \sa operator-=(), operator+()
  1212. */
  1213. /*! \fn QHash::const_iterator &QHash::const_iterator::operator-=(int j)
  1214. Makes the iterator go back by \a j items. (If \a j is negative,
  1215. the iterator goes forward.)
  1216. This operation can be slow for large \a j values.
  1217. \sa operator+=(), operator-()
  1218. */
  1219. /*! \fn QDataStream &operator<<(QDataStream &out, const QHash<Key, T>& hash)
  1220. \relates QHash
  1221. Writes the hash \a hash to stream \a out.
  1222. This function requires the key and value types to implement \c
  1223. operator<<().
  1224. \sa {Serializing Qt Data Types}
  1225. */
  1226. /*! \fn QDataStream &operator>>(QDataStream &in, QHash<Key, T> &hash)
  1227. \relates QHash
  1228. Reads a hash from stream \a in into \a hash.
  1229. This function requires the key and value types to implement \c
  1230. operator>>().
  1231. \sa {Serializing Qt Data Types}
  1232. */
  1233. /*! \class QMultiHash
  1234. \brief The QMultiHash class is a convenience QHash subclass that provides multi-valued hashes.
  1235. \ingroup tools
  1236. \ingroup shared
  1237. \reentrant
  1238. QMultiHash\<Key, T\> is one of Qt's generic \l{container classes}.
  1239. It inherits QHash and extends it with a few convenience functions
  1240. that make it more suitable than QHash for storing multi-valued
  1241. hashes. A multi-valued hash is a hash that allows multiple values
  1242. with the same key; QHash normally doesn't allow that, unless you
  1243. call QHash::insertMulti().
  1244. Because QMultiHash inherits QHash, all of QHash's functionality also
  1245. applies to QMultiHash. For example, you can use isEmpty() to test
  1246. whether the hash is empty, and you can traverse a QMultiHash using
  1247. QHash's iterator classes (for example, QHashIterator). But in
  1248. addition, it provides an insert() function that corresponds to
  1249. QHash::insertMulti(), and a replace() function that corresponds to
  1250. QHash::insert(). It also provides convenient operator+() and
  1251. operator+=().
  1252. Example:
  1253. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 24
  1254. Unlike QHash, QMultiHash provides no operator[]. Use value() or
  1255. replace() if you want to access the most recently inserted item
  1256. with a certain key.
  1257. If you want to retrieve all the values for a single key, you can
  1258. use values(const Key &key), which returns a QList<T>:
  1259. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 25
  1260. The items that share the same key are available from most
  1261. recently to least recently inserted.
  1262. A more efficient approach is to call find() to get
  1263. the STL-style iterator for the first item with a key and iterate from
  1264. there:
  1265. \snippet doc/src/snippets/code/src_corelib_tools_qhash.cpp 26
  1266. QMultiHash's key and value data types must be \l{assignable data
  1267. types}. You cannot, for example, store a QWidget as a value;
  1268. instead, store a QWidget *. In addition, QMultiHash's key type
  1269. must provide operator==(), and there must also be a global
  1270. qHash() function that returns a hash value for an argument of the
  1271. key's type. See the QHash documentation for details.
  1272. \sa QHash, QHashIterator, QMutableHashIterator, QMultiMap
  1273. */
  1274. /*! \fn QMultiHash::QMultiHash()
  1275. Constructs an empty hash.
  1276. */
  1277. /*! \fn QMultiHash::QMultiHash(const QHash<Key, T> &other)
  1278. Constructs a copy of \a other (which can be a QHash or a
  1279. QMultiHash).
  1280. \sa operator=()
  1281. */
  1282. /*! \fn QMultiHash::iterator QMultiHash::replace(const Key &key, const T &value)
  1283. Inserts a new item with the \a key and a value of \a value.
  1284. If there is already an item with the \a key, that item's value
  1285. is replaced with \a value.
  1286. If there are multiple items with the \a key, the most
  1287. recently inserted item's value is replaced with \a value.
  1288. \sa insert()
  1289. */
  1290. /*! \fn QMultiHash::iterator QMultiHash::insert(const Key &key, const T &value)
  1291. Inserts a new item with the \a key and a value of \a value.
  1292. If there is already an item with the same key in the hash, this
  1293. function will simply create a new one. (This behavior is
  1294. different from replace(), which overwrites the value of an
  1295. existing item.)
  1296. \sa replace()
  1297. */
  1298. /*! \fn QMultiHash &QMultiHash::operator+=(const QMultiHash &other)
  1299. Inserts all the items in the \a other hash into this hash
  1300. and returns a reference to this hash.
  1301. \sa insert()
  1302. */
  1303. /*! \fn QMultiHash QMultiHash::operator+(const QMultiHash &other) const
  1304. Returns a hash that contains all the items in this hash in
  1305. addition to all the items in \a other. If a key is common to both
  1306. hashes, the resulting hash will contain the key multiple times.
  1307. \sa operator+=()
  1308. */
  1309. /*!
  1310. \fn bool QMultiHash::contains(const Key &key, const T &value) const
  1311. \since 4.3
  1312. Returns true if the hash contains an item with the \a key and
  1313. \a value; otherwise returns false.
  1314. \sa QHash::contains()
  1315. */
  1316. /*!
  1317. \fn bool QMultiHash::contains(const Key &key) const
  1318. \overload
  1319. \sa QHash::contains()
  1320. */
  1321. /*!
  1322. \fn int QMultiHash::remove(const Key &key, const T &value)
  1323. \since 4.3
  1324. Removes all the items that have the \a key and the value \a
  1325. value from the hash. Returns the number of items removed.
  1326. \sa QHash::remove()
  1327. */
  1328. /*!
  1329. \fn int QMultiHash::remove(const Key &key)
  1330. \overload
  1331. \sa QHash::remove()
  1332. */
  1333. /*!
  1334. \fn int QMultiHash::count(const Key &key, const T &value) const
  1335. \since 4.3
  1336. Returns the number of items with the \a key and \a value.
  1337. \sa QHash::count()
  1338. */
  1339. /*!
  1340. \fn int QMultiHash::count(const Key &key) const
  1341. \overload
  1342. \sa QHash::count()
  1343. */
  1344. /*!
  1345. \fn int QMultiHash::count() const
  1346. \overload
  1347. \sa QHash::count()
  1348. */
  1349. /*!
  1350. \fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key, const T &value)
  1351. \since 4.3
  1352. Returns an iterator pointing to the item with the \a key and \a value.
  1353. If the hash contains no such item, the function returns end().
  1354. If the hash contains multiple items with the \a key and \a value, the
  1355. iterator returned points to the most recently inserted item.
  1356. \sa QHash::find()
  1357. */
  1358. /*!
  1359. \fn typename QHash<Key, T>::iterator QMultiHash::find(const Key &key)
  1360. \overload
  1361. \sa QHash::find()
  1362. */
  1363. /*!
  1364. \fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key, const T &value) const
  1365. \since 4.3
  1366. \overload
  1367. */
  1368. /*!
  1369. \fn typename QHash<Key, T>::const_iterator QMultiHash::find(const Key &key) const
  1370. \overload
  1371. \sa QHash::find()
  1372. */
  1373. /*!
  1374. \fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key, const T &value) const
  1375. \since 4.3
  1376. Returns an iterator pointing to the item with the \a key and the
  1377. \a value in the hash.
  1378. If the hash contains no such item, the function returns
  1379. constEnd().
  1380. \sa QHash::constFind()
  1381. */
  1382. /*!
  1383. \fn typename QHash<Key, T>::const_iterator QMultiHash::constFind(const Key &key) const
  1384. \overload
  1385. \sa QHash::constFind()
  1386. */
  1387. QT_END_NAMESPACE