1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
53 const QMapDataBase QMapDataBase::shared_null = { Q_REFCOUNT_INITIALIZE_STATIC, 0, { 0, 0, 0 } };
55 const QMapNodeBase *QMapNodeBase::nextNode() const
57 const QMapNodeBase *n = this;
63 const QMapNodeBase *y = n->parent();
64 while (y && n == y->right) {
73 const QMapNodeBase *QMapNodeBase::previousNode() const
75 const QMapNodeBase *n = this;
81 const QMapNodeBase *y = n->parent();
82 while (y && n == y->left) {
92 void QMapDataBase::rotateLeft(QMapNodeBase *x)
94 QMapNodeBase *&root = header.left;
95 QMapNodeBase *y = x->right;
98 y->left->setParent(x);
99 y->setParent(x->parent());
102 else if (x == x->parent()->left)
103 x->parent()->left = y;
105 x->parent()->right = y;
111 void QMapDataBase::rotateRight(QMapNodeBase *x)
113 QMapNodeBase *&root = header.left;
114 QMapNodeBase *y = x->left;
117 y->right->setParent(x);
118 y->setParent(x->parent());
121 else if (x == x->parent()->right)
122 x->parent()->right = y;
124 x->parent()->left = y;
130 void QMapDataBase::rebalance(QMapNodeBase *x)
132 QMapNodeBase *&root = header.left;
133 x->setColor(QMapNodeBase::Red);
134 while (x != root && x->parent()->color() == QMapNodeBase::Red) {
135 if (x->parent() == x->parent()->parent()->left) {
136 QMapNodeBase *y = x->parent()->parent()->right;
137 if (y && y->color() == QMapNodeBase::Red) {
138 x->parent()->setColor(QMapNodeBase::Black);
139 y->setColor(QMapNodeBase::Black);
140 x->parent()->parent()->setColor(QMapNodeBase::Red);
141 x = x->parent()->parent();
143 if (x == x->parent()->right) {
147 x->parent()->setColor(QMapNodeBase::Black);
148 x->parent()->parent()->setColor(QMapNodeBase::Red);
149 rotateRight (x->parent()->parent());
152 QMapNodeBase *y = x->parent()->parent()->left;
153 if (y && y->color() == QMapNodeBase::Red) {
154 x->parent()->setColor(QMapNodeBase::Black);
155 y->setColor(QMapNodeBase::Black);
156 x->parent()->parent()->setColor(QMapNodeBase::Red);
157 x = x->parent()->parent();
159 if (x == x->parent()->left) {
163 x->parent()->setColor(QMapNodeBase::Black);
164 x->parent()->parent()->setColor(QMapNodeBase::Red);
165 rotateLeft(x->parent()->parent());
169 root->setColor(QMapNodeBase::Black);
172 void QMapDataBase::freeNodeAndRebalance(QMapNodeBase *z)
174 QMapNodeBase *&root = header.left;
177 QMapNodeBase *x_parent;
191 z->left->setParent(y);
194 x_parent = y->parent();
196 x->setParent(y->parent());
197 y->parent()->left = x;
199 z->right->setParent(y);
205 else if (z->parent()->left == z)
206 z->parent()->left = y;
208 z->parent()->right = y;
209 y->setParent(z->parent());
211 QMapNodeBase::Color c = y->color();
212 y->setColor(z->color());
216 x_parent = y->parent();
218 x->setParent(y->parent());
221 else if (z->parent()->left == z)
222 z->parent()->left = x;
224 z->parent()->right = x;
226 if (y->color() != QMapNodeBase::Red) {
227 while (x != root && (x == 0 || x->color() == QMapNodeBase::Black)) {
228 if (x == x_parent->left) {
229 QMapNodeBase *w = x_parent->right;
230 if (w->color() == QMapNodeBase::Red) {
231 w->setColor(QMapNodeBase::Black);
232 x_parent->setColor(QMapNodeBase::Red);
233 rotateLeft(x_parent);
236 if ((w->left == 0 || w->left->color() == QMapNodeBase::Black) &&
237 (w->right == 0 || w->right->color() == QMapNodeBase::Black)) {
238 w->setColor(QMapNodeBase::Red);
240 x_parent = x_parent->parent();
242 if (w->right == 0 || w->right->color() == QMapNodeBase::Black) {
244 w->left->setColor(QMapNodeBase::Black);
245 w->setColor(QMapNodeBase::Red);
249 w->setColor(x_parent->color());
250 x_parent->setColor(QMapNodeBase::Black);
252 w->right->setColor(QMapNodeBase::Black);
253 rotateLeft(x_parent);
257 QMapNodeBase *w = x_parent->left;
258 if (w->color() == QMapNodeBase::Red) {
259 w->setColor(QMapNodeBase::Black);
260 x_parent->setColor(QMapNodeBase::Red);
261 rotateRight(x_parent);
264 if ((w->right == 0 || w->right->color() == QMapNodeBase::Black) &&
265 (w->left == 0 || w->left->color() == QMapNodeBase::Black)) {
266 w->setColor(QMapNodeBase::Red);
268 x_parent = x_parent->parent();
270 if (w->left == 0 || w->left->color() == QMapNodeBase::Black) {
272 w->right->setColor(QMapNodeBase::Black);
273 w->setColor(QMapNodeBase::Red);
277 w->setColor(x_parent->color());
278 x_parent->setColor(QMapNodeBase::Black);
280 w->left->setColor(QMapNodeBase::Black);
281 rotateRight(x_parent);
287 x->setColor(QMapNodeBase::Black);
293 static inline int qMapAlignmentThreshold()
295 // malloc on 32-bit platforms should return pointers that are 8-byte
296 // aligned or more while on 64-bit platforms they should be 16-byte aligned
298 return 2 * sizeof(void*);
301 static inline void *qMapAllocate(int alloc, int alignment)
303 return alignment > qMapAlignmentThreshold()
304 ? qMallocAligned(alloc, alignment)
308 static inline void qMapDeallocate(QMapNodeBase *node, int alignment)
310 if (alignment > qMapAlignmentThreshold())
316 QMapNodeBase *QMapDataBase::createNode(int alloc, int alignment, QMapNodeBase *parent, bool left)
318 QMapNodeBase *node = static_cast<QMapNodeBase *>(qMapAllocate(alloc, alignment));
321 memset(node, 0, alloc);
328 parent->right = node;
330 node->setParent(parent);
336 void QMapDataBase::freeTree(QMapNodeBase *root, int alignment)
339 freeTree(root->left, alignment);
341 freeTree(root->right, alignment);
342 qMapDeallocate(root, alignment);
345 QMapDataBase *QMapDataBase::createData()
347 QMapDataBase *d = new QMapDataBase;
349 d->ref.initializeOwned();
359 void QMapDataBase::freeData(QMapDataBase *d)
366 \brief The QMap class is a template class that provides a skip-list-based dictionary.
373 QMap\<Key, T\> is one of Qt's generic \l{container classes}. It
374 stores (key, value) pairs and provides fast lookup of the
375 value associated with a key.
377 QMap and QHash provide very similar functionality. The
381 \li QHash provides faster lookups than QMap. (See \l{Algorithmic
382 Complexity} for details.)
383 \li When iterating over a QHash, the items are arbitrarily ordered.
384 With QMap, the items are always sorted by key.
385 \li The key type of a QHash must provide operator==() and a global
386 qHash(Key) function. The key type of a QMap must provide
387 operator<() specifying a total order.
390 Here's an example QMap with QString keys and \c int values:
391 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 0
393 To insert a (key, value) pair into the map, you can use operator[]():
395 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 1
397 This inserts the following three (key, value) pairs into the
398 QMap: ("one", 1), ("three", 3), and ("seven", 7). Another way to
399 insert items into the map is to use insert():
401 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 2
403 To look up a value, use operator[]() or value():
405 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 3
407 If there is no item with the specified key in the map, these
408 functions return a \l{default-constructed value}.
410 If you want to check whether the map contains a certain key, use
413 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 4
415 There is also a value() overload that uses its second argument as
416 a default value if there is no item with the specified key:
418 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 5
420 In general, we recommend that you use contains() and value()
421 rather than operator[]() for looking up a key in a map. The
422 reason is that operator[]() silently inserts an item into the
423 map if no item exists with the same key (unless the map is
424 const). For example, the following code snippet will create 1000
427 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 6
429 To avoid this problem, replace \c map[i] with \c map.value(i)
432 If you want to navigate through all the (key, value) pairs stored
433 in a QMap, you can use an iterator. QMap provides both
434 \l{Java-style iterators} (QMapIterator and QMutableMapIterator)
435 and \l{STL-style iterators} (QMap::const_iterator and
436 QMap::iterator). Here's how to iterate over a QMap<QString, int>
437 using a Java-style iterator:
439 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 7
441 Here's the same code, but using an STL-style iterator this time:
443 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 8
445 The items are traversed in ascending key order.
447 Normally, a QMap allows only one value per key. If you call
448 insert() with a key that already exists in the QMap, the
449 previous value will be erased. For example:
451 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 9
453 However, you can store multiple values per key by using
454 insertMulti() instead of insert() (or using the convenience
455 subclass QMultiMap). If you want to retrieve all the values for a
456 single key, you can use values(const Key &key), which returns a
459 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 10
461 The items that share the same key are available from most
462 recently to least recently inserted. Another approach is to call
463 find() to get the STL-style iterator for the first item with a
464 key and iterate from there:
466 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 11
468 If you only need to extract the values from a map (not the keys),
469 you can also use \l{foreach}:
471 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 12
473 Items can be removed from the map in several ways. One way is to
474 call remove(); this will remove any item with the given key.
475 Another way is to use QMutableMapIterator::remove(). In addition,
476 you can clear the entire map using clear().
478 QMap's key and value data types must be \l{assignable data
479 types}. This covers most data types you are likely to encounter,
480 but the compiler won't let you, for example, store a QWidget as a
481 value; instead, store a QWidget *. In addition, QMap's key type
482 must provide operator<(). QMap uses it to keep its items sorted,
483 and assumes that two keys \c x and \c y are equal if neither \c{x
484 < y} nor \c{y < x} is true.
487 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 13
489 In the example, we start by comparing the employees' names. If
490 they're equal, we compare their dates of birth to break the tie.
492 \sa QMapIterator, QMutableMapIterator, QHash, QSet
497 Constructs an empty map.
502 /*! \fn QMap::QMap(const QMap<Key, T> &other)
504 Constructs a copy of \a other.
506 This operation occurs in \l{constant time}, because QMap is
507 \l{implicitly shared}. This makes returning a QMap from a
508 function very fast. If a shared instance is modified, it will be
509 copied (copy-on-write), and this takes \l{linear time}.
514 /*! \fn QMap::QMap(const std::map<Key, T> & other)
516 Constructs a copy of \a other.
518 This function is only available if Qt is configured with STL
519 compatibility enabled.
524 /*! \fn std::map<Key, T> QMap::toStdMap() const
526 Returns an STL map equivalent to this QMap.
528 This function is only available if Qt is configured with STL
529 compatibility enabled.
532 /*! \fn QMap::~QMap()
534 Destroys the map. References to the values in the map, and all
535 iterators over this map, become invalid.
538 /*! \fn QMap<Key, T> &QMap::operator=(const QMap<Key, T> &other)
540 Assigns \a other to this map and returns a reference to this map.
543 /*! \fn void QMap::swap(QMap<Key, T> &other)
546 Swaps map \a other with this map. This operation is very
547 fast and never fails.
550 /*! \fn void QMultiMap::swap(QMultiMap<Key, T> &other)
553 Swaps map \a other with this map. This operation is very
554 fast and never fails.
557 /*! \fn bool QMap::operator==(const QMap<Key, T> &other) const
559 Returns true if \a other is equal to this map; otherwise returns
562 Two maps are considered equal if they contain the same (key,
565 This function requires the value type to implement \c
571 /*! \fn bool QMap::operator!=(const QMap<Key, T> &other) const
573 Returns true if \a other is not equal to this map; otherwise
576 Two maps are considered equal if they contain the same (key,
579 This function requires the value type to implement \c
585 /*! \fn int QMap::size() const
587 Returns the number of (key, value) pairs in the map.
589 \sa isEmpty(), count()
593 \fn bool QMap::isEmpty() const
595 Returns true if the map contains no items; otherwise returns
601 /*! \fn void QMap::detach()
605 Detaches this map from any other maps with which it may share
611 /*! \fn bool QMap::isDetached() const
615 Returns true if the map's internal data isn't shared with any
616 other map object; otherwise returns false.
621 /*! \fn void QMap::setSharable(bool sharable)
626 /*! \fn bool QMap::isSharedWith(const QMap<Key, T> &other) const
631 /*! \fn void QMap::clear()
633 Removes all items from the map.
638 /*! \fn int QMap::remove(const Key &key)
640 Removes all the items that have the key \a key from the map.
641 Returns the number of items removed which is usually 1 but will be
642 0 if the key isn't in the map, or \> 1 if insertMulti() has been
643 used with the \a key.
645 \sa clear(), take(), QMultiMap::remove()
648 /*! \fn T QMap::take(const Key &key)
650 Removes the item with the key \a key from the map and returns
651 the value associated with it.
653 If the item does not exist in the map, the function simply
654 returns a \l{default-constructed value}. If there are multiple
655 items for \a key in the map, only the most recently inserted one
656 is removed and returned.
658 If you don't use the return value, remove() is more efficient.
663 /*! \fn bool QMap::contains(const Key &key) const
665 Returns true if the map contains an item with key \a key;
666 otherwise returns false.
668 \sa count(), QMultiMap::contains()
671 /*! \fn const T QMap::value(const Key &key) const
675 /*! \fn const T QMap::value(const Key &key, const T &defaultValue) const
679 Returns the value associated with the key \a key.
681 If the map contains no item with key \a key, the function returns
682 \a defaultValue. If no \a defaultValue is specified, the function
683 returns a \l{default-constructed value}. If there are multiple
684 items for \a key in the map, the value of the most recently
685 inserted one is returned.
687 \sa key(), values(), contains(), operator[]()
690 /*! \fn T &QMap::operator[](const Key &key)
692 Returns the value associated with the key \a key as a modifiable
695 If the map contains no item with key \a key, the function inserts
696 a \l{default-constructed value} into the map with key \a key, and
697 returns a reference to it. If the map contains multiple items
698 with key \a key, this function returns a reference to the most
699 recently inserted value.
701 \sa insert(), value()
704 /*! \fn const T QMap::operator[](const Key &key) const
711 /*! \fn QList<Key> QMap::uniqueKeys() const
714 Returns a list containing all the keys in the map in ascending
715 order. Keys that occur multiple times in the map (because items
716 were inserted with insertMulti(), or unite() was used) occur only
717 once in the returned list.
722 /*! \fn QList<Key> QMap::keys() const
724 Returns a list containing all the keys in the map in ascending
725 order. Keys that occur multiple times in the map (because items
726 were inserted with insertMulti(), or unite() was used) also
727 occur multiple times in the list.
729 To obtain a list of unique keys, where each key from the map only
730 occurs once, use uniqueKeys().
732 The order is guaranteed to be the same as that used by values().
734 \sa uniqueKeys(), values(), key()
737 /*! \fn QList<Key> QMap::keys(const T &value) const
741 Returns a list containing all the keys associated with value \a
742 value in ascending order.
744 This function can be slow (\l{linear time}), because QMap's
745 internal data structure is optimized for fast lookup by key, not
750 \fn Key QMap::key(const T &value, const Key &defaultKey) const
754 Returns the first key with value \a value, or \a defaultKey if
755 the map contains no item with value \a value. If no \a defaultKey
756 is provided the function returns a \link {default-constructed value}
757 default-constructed key \endlink.
759 This function can be slow (\l{linear time}), because QMap's
760 internal data structure is optimized for fast lookup by key, not
766 /*! \fn QList<T> QMap::values() const
768 Returns a list containing all the values in the map, in ascending
769 order of their keys. If a key is associated with multiple values,
770 all of its values will be in the list, and not just the most
771 recently inserted one.
776 /*! \fn QList<T> QMap::values(const Key &key) const
780 Returns a list containing all the values associated with key
781 \a key, from the most recently inserted to the least recently
784 \sa count(), insertMulti()
787 /*! \fn int QMap::count(const Key &key) const
789 Returns the number of items associated with key \a key.
791 \sa contains(), insertMulti(), QMultiMap::count()
794 /*! \fn int QMap::count() const
801 /*! \fn QMap::iterator QMap::begin()
803 Returns an \l{STL-style iterator} pointing to the first item in
806 \sa constBegin(), end()
809 /*! \fn QMap::const_iterator QMap::begin() const
814 /*! \fn QMap::const_iterator QMap::cbegin() const
817 Returns a const \l{STL-style iterator} pointing to the first item
823 /*! \fn QMap::const_iterator QMap::constBegin() const
825 Returns a const \l{STL-style iterator} pointing to the first item
828 \sa begin(), constEnd()
831 /*! \fn QMap::iterator QMap::end()
833 Returns an \l{STL-style iterator} pointing to the imaginary item
834 after the last item in the map.
836 \sa begin(), constEnd()
839 /*! \fn QMap::const_iterator QMap::end() const
844 /*! \fn QMap::const_iterator QMap::cend() const
847 Returns a const \l{STL-style iterator} pointing to the imaginary
848 item after the last item in the map.
853 /*! \fn QMap::const_iterator QMap::constEnd() const
855 Returns a const \l{STL-style iterator} pointing to the imaginary
856 item after the last item in the map.
858 \sa constBegin(), end()
861 /*! \fn QMap::iterator QMap::erase(iterator pos)
863 Removes the (key, value) pair pointed to by the iterator \a pos
864 from the map, and returns an iterator to the next item in the
870 /*! \fn QMap::iterator QMap::find(const Key &key)
872 Returns an iterator pointing to the item with key \a key in the
875 If the map contains no item with key \a key, the function
878 If the map contains multiple items with key \a key, this
879 function returns an iterator that points to the most recently
880 inserted value. The other values are accessible by incrementing
881 the iterator. For example, here's some code that iterates over all
882 the items with the same key:
884 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 14
886 \sa constFind(), value(), values(), lowerBound(), upperBound(), QMultiMap::find()
889 /*! \fn QMap::const_iterator QMap::find(const Key &key) const
894 /*! \fn QMap::const_iterator QMap::constFind(const Key &key) const
897 Returns an const iterator pointing to the item with key \a key in the
900 If the map contains no item with key \a key, the function
903 \sa find(), QMultiMap::constFind()
906 /*! \fn QMap::iterator QMap::lowerBound(const Key &key)
908 Returns an iterator pointing to the first item with key \a key in
909 the map. If the map contains no item with key \a key, the
910 function returns an iterator to the nearest item with a greater
914 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 15
916 If the map contains multiple items with key \a key, this
917 function returns an iterator that points to the most recently
918 inserted value. The other values are accessible by incrementing
919 the iterator. For example, here's some code that iterates over all
920 the items with the same key:
922 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 16
924 \sa qLowerBound(), upperBound(), find()
927 /*! \fn QMap::const_iterator QMap::lowerBound(const Key &key) const
932 /*! \fn QMap::iterator QMap::upperBound(const Key &key)
934 Returns an iterator pointing to the item that immediately follows
935 the last item with key \a key in the map. If the map contains no
936 item with key \a key, the function returns an iterator to the
937 nearest item with a greater key.
940 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 17
942 \sa qUpperBound(), lowerBound(), find()
945 /*! \fn QMap::const_iterator QMap::upperBound(const Key &key) const
950 /*! \fn QMap::iterator QMap::insert(const Key &key, const T &value)
952 Inserts a new item with the key \a key and a value of \a value.
954 If there is already an item with the key \a key, that item's value
955 is replaced with \a value.
957 If there are multiple items with the key \a key, the most
958 recently inserted item's value is replaced with \a value.
963 /*! \fn QMap::iterator QMap::insertMulti(const Key &key, const T &value)
965 Inserts a new item with the key \a key and a value of \a value.
967 If there is already an item with the same key in the map, this
968 function will simply create a new one. (This behavior is
969 different from insert(), which overwrites the value of an
972 \sa insert(), values()
975 /*! \fn QMap<Key, T> &QMap::unite(const QMap<Key, T> &other)
977 Inserts all the items in the \a other map into this map. If a
978 key is common to both maps, the resulting map will contain the
984 /*! \typedef QMap::Iterator
986 Qt-style synonym for QMap::iterator.
989 /*! \typedef QMap::ConstIterator
991 Qt-style synonym for QMap::const_iterator.
994 /*! \typedef QMap::difference_type
996 Typedef for ptrdiff_t. Provided for STL compatibility.
999 /*! \typedef QMap::key_type
1001 Typedef for Key. Provided for STL compatibility.
1004 /*! \typedef QMap::mapped_type
1006 Typedef for T. Provided for STL compatibility.
1009 /*! \typedef QMap::size_type
1011 Typedef for int. Provided for STL compatibility.
1015 \fn bool QMap::empty() const
1017 This function is provided for STL compatibility. It is equivalent
1018 to isEmpty(), returning true if the map is empty; otherwise
1022 /*! \class QMap::iterator
1023 \brief The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
1025 QMap features both \l{STL-style iterators} and \l{Java-style
1026 iterators}. The STL-style iterators are more low-level and more
1027 cumbersome to use; on the other hand, they are slightly faster
1028 and, for developers who already know STL, have the advantage of
1031 QMap\<Key, T\>::iterator allows you to iterate over a QMap (or
1032 QMultiMap) and to modify the value (but not the key) stored under
1033 a particular key. If you want to iterate over a const QMap, you
1034 should use QMap::const_iterator. It is generally good practice to
1035 use QMap::const_iterator on a non-const QMap as well, unless you
1036 need to change the QMap through the iterator. Const iterators are
1037 slightly faster, and can improve code readability.
1039 The default QMap::iterator constructor creates an uninitialized
1040 iterator. You must initialize it using a QMap function like
1041 QMap::begin(), QMap::end(), or QMap::find() before you can
1042 start iterating. Here's a typical loop that prints all the (key,
1043 value) pairs stored in a map:
1045 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 18
1047 Unlike QHash, which stores its items in an arbitrary order, QMap
1048 stores its items ordered by key. Items that share the same key
1049 (because they were inserted using QMap::insertMulti(), or due to a
1050 unite()) will appear consecutively, from the most recently to the
1051 least recently inserted value.
1053 Let's see a few examples of things we can do with a
1054 QMap::iterator that we cannot do with a QMap::const_iterator.
1055 Here's an example that increments every value stored in the QMap
1058 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 19
1060 Here's an example that removes all the items whose key is a
1061 string that starts with an underscore character:
1063 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 20
1065 The call to QMap::erase() removes the item pointed to by the
1066 iterator from the map, and returns an iterator to the next item.
1067 Here's another way of removing an item while iterating:
1069 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 21
1071 It might be tempting to write code like this:
1073 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 22
1075 However, this will potentially crash in \c{++i}, because \c i is
1076 a dangling iterator after the call to erase().
1078 Multiple iterators can be used on the same map. If you add items
1079 to the map, existing iterators will remain valid. If you remove
1080 items from the map, iterators that point to the removed items
1081 will become dangling iterators.
1083 \sa QMap::const_iterator, QMutableMapIterator
1086 /*! \fn QMap::iterator::operator QMapData::Node *() const
1091 /*! \typedef QMap::iterator::difference_type
1096 /*! \typedef QMap::iterator::iterator_category
1098 A synonym for \e {std::bidirectional_iterator_tag} indicating
1099 this iterator is a bidirectional iterator.
1102 /*! \typedef QMap::iterator::pointer
1107 /*! \typedef QMap::iterator::reference
1112 /*! \typedef QMap::iterator::value_type
1117 /*! \fn QMap::iterator::iterator()
1119 Constructs an uninitialized iterator.
1121 Functions like key(), value(), and operator++() must not be
1122 called on an uninitialized iterator. Use operator=() to assign a
1123 value to it before using it.
1125 \sa QMap::begin() QMap::end()
1128 /*! \fn QMap::iterator::iterator(QMapData::Node *node)
1133 /*! \fn const Key &QMap::iterator::key() const
1135 Returns the current item's key as a const reference.
1137 There is no direct way of changing an item's key through an
1138 iterator, although it can be done by calling QMap::erase()
1139 followed by QMap::insert() or QMap::insertMulti().
1144 /*! \fn T &QMap::iterator::value() const
1146 Returns a modifiable reference to the current item's value.
1148 You can change the value of an item by using value() on
1149 the left side of an assignment, for example:
1151 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 23
1153 \sa key(), operator*()
1156 /*! \fn T &QMap::iterator::operator*() const
1158 Returns a modifiable reference to the current item's value.
1165 /*! \fn T *QMap::iterator::operator->() const
1167 Returns a pointer to the current item's value.
1173 \fn bool QMap::iterator::operator==(const iterator &other) const
1174 \fn bool QMap::iterator::operator==(const const_iterator &other) const
1176 Returns true if \a other points to the same item as this
1177 iterator; otherwise returns false.
1183 \fn bool QMap::iterator::operator!=(const iterator &other) const
1184 \fn bool QMap::iterator::operator!=(const const_iterator &other) const
1186 Returns true if \a other points to a different item than this
1187 iterator; otherwise returns false.
1192 /*! \fn QMap::iterator QMap::iterator::operator++()
1194 The prefix ++ operator (\c{++i}) advances the iterator to the
1195 next item in the map and returns an iterator to the new current
1198 Calling this function on QMap::end() leads to undefined results.
1203 /*! \fn QMap::iterator QMap::iterator::operator++(int)
1207 The postfix ++ operator (\c{i++}) advances the iterator to the
1208 next item in the map and returns an iterator to the previously
1212 /*! \fn QMap::iterator QMap::iterator::operator--()
1214 The prefix -- operator (\c{--i}) makes the preceding item
1215 current and returns an iterator pointing to the new current item.
1217 Calling this function on QMap::begin() leads to undefined
1223 /*! \fn QMap::iterator QMap::iterator::operator--(int)
1227 The postfix -- operator (\c{i--}) makes the preceding item
1228 current and returns an iterator pointing to the previously
1232 /*! \fn QMap::iterator QMap::iterator::operator+(int j) const
1234 Returns an iterator to the item at \a j positions forward from
1235 this iterator. (If \a j is negative, the iterator goes backward.)
1237 This operation can be slow for large \a j values.
1243 /*! \fn QMap::iterator QMap::iterator::operator-(int j) const
1245 Returns an iterator to the item at \a j positions backward from
1246 this iterator. (If \a j is negative, the iterator goes forward.)
1248 This operation can be slow for large \a j values.
1253 /*! \fn QMap::iterator &QMap::iterator::operator+=(int j)
1255 Advances the iterator by \a j items. (If \a j is negative, the
1256 iterator goes backward.)
1258 \sa operator-=(), operator+()
1261 /*! \fn QMap::iterator &QMap::iterator::operator-=(int j)
1263 Makes the iterator go back by \a j items. (If \a j is negative,
1264 the iterator goes forward.)
1266 \sa operator+=(), operator-()
1269 /*! \class QMap::const_iterator
1270 \brief The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
1272 QMap features both \l{STL-style iterators} and \l{Java-style
1273 iterators}. The STL-style iterators are more low-level and more
1274 cumbersome to use; on the other hand, they are slightly faster
1275 and, for developers who already know STL, have the advantage of
1278 QMap\<Key, T\>::const_iterator allows you to iterate over a QMap
1279 (or a QMultiMap). If you want to modify the QMap as you iterate
1280 over it, you must use QMap::iterator instead. It is generally
1281 good practice to use QMap::const_iterator on a non-const QMap as
1282 well, unless you need to change the QMap through the iterator.
1283 Const iterators are slightly faster, and can improve code
1286 The default QMap::const_iterator constructor creates an
1287 uninitialized iterator. You must initialize it using a QMap
1288 function like QMap::constBegin(), QMap::constEnd(), or
1289 QMap::find() before you can start iterating. Here's a typical
1290 loop that prints all the (key, value) pairs stored in a map:
1292 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 24
1294 Unlike QHash, which stores its items in an arbitrary order, QMap
1295 stores its items ordered by key. Items that share the same key
1296 (because they were inserted using QMap::insertMulti()) will
1297 appear consecutively, from the most recently to the least
1298 recently inserted value.
1300 Multiple iterators can be used on the same map. If you add items
1301 to the map, existing iterators will remain valid. If you remove
1302 items from the map, iterators that point to the removed items
1303 will become dangling iterators.
1305 \sa QMap::iterator, QMapIterator
1308 /*! \fn QMap::const_iterator::operator QMapData::Node *() const
1313 /*! \typedef QMap::const_iterator::difference_type
1318 /*! \typedef QMap::const_iterator::iterator_category
1320 A synonym for \e {std::bidirectional_iterator_tag} indicating
1321 this iterator is a bidirectional iterator.
1324 /*! \typedef QMap::const_iterator::pointer
1329 /*! \typedef QMap::const_iterator::reference
1334 /*! \typedef QMap::const_iterator::value_type
1339 /*! \fn QMap::const_iterator::const_iterator()
1341 Constructs an uninitialized iterator.
1343 Functions like key(), value(), and operator++() must not be
1344 called on an uninitialized iterator. Use operator=() to assign a
1345 value to it before using it.
1347 \sa QMap::constBegin() QMap::constEnd()
1350 /*! \fn QMap::const_iterator::const_iterator(QMapData::Node *node)
1355 /*! \fn QMap::const_iterator::const_iterator(const iterator &other)
1357 Constructs a copy of \a other.
1360 /*! \fn const Key &QMap::const_iterator::key() const
1362 Returns the current item's key.
1367 /*! \fn const T &QMap::const_iterator::value() const
1369 Returns the current item's value.
1371 \sa key(), operator*()
1374 /*! \fn const T &QMap::const_iterator::operator*() const
1376 Returns the current item's value.
1383 /*! \fn const T *QMap::const_iterator::operator->() const
1385 Returns a pointer to the current item's value.
1390 /*! \fn bool QMap::const_iterator::operator==(const const_iterator &other) const
1392 Returns true if \a other points to the same item as this
1393 iterator; otherwise returns false.
1398 /*! \fn bool QMap::const_iterator::operator!=(const const_iterator &other) const
1400 Returns true if \a other points to a different item than this
1401 iterator; otherwise returns false.
1406 /*! \fn QMap::const_iterator QMap::const_iterator::operator++()
1408 The prefix ++ operator (\c{++i}) advances the iterator to the
1409 next item in the map and returns an iterator to the new current
1412 Calling this function on QMap::end() leads to undefined results.
1417 /*! \fn QMap::const_iterator QMap::const_iterator::operator++(int)
1421 The postfix ++ operator (\c{i++}) advances the iterator to the
1422 next item in the map and returns an iterator to the previously
1426 /*! \fn QMap::const_iterator &QMap::const_iterator::operator--()
1428 The prefix -- operator (\c{--i}) makes the preceding item
1429 current and returns an iterator pointing to the new current item.
1431 Calling this function on QMap::begin() leads to undefined
1437 /*! \fn QMap::const_iterator QMap::const_iterator::operator--(int)
1441 The postfix -- operator (\c{i--}) makes the preceding item
1442 current and returns an iterator pointing to the previously
1446 /*! \fn QMap::const_iterator QMap::const_iterator::operator+(int j) const
1448 Returns an iterator to the item at \a j positions forward from
1449 this iterator. (If \a j is negative, the iterator goes backward.)
1451 This operation can be slow for large \a j values.
1456 /*! \fn QMap::const_iterator QMap::const_iterator::operator-(int j) const
1458 Returns an iterator to the item at \a j positions backward from
1459 this iterator. (If \a j is negative, the iterator goes forward.)
1461 This operation can be slow for large \a j values.
1466 /*! \fn QMap::const_iterator &QMap::const_iterator::operator+=(int j)
1468 Advances the iterator by \a j items. (If \a j is negative, the
1469 iterator goes backward.)
1471 This operation can be slow for large \a j values.
1473 \sa operator-=(), operator+()
1476 /*! \fn QMap::const_iterator &QMap::const_iterator::operator-=(int j)
1478 Makes the iterator go back by \a j items. (If \a j is negative,
1479 the iterator goes forward.)
1481 This operation can be slow for large \a j values.
1483 \sa operator+=(), operator-()
1486 /*! \fn QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
1489 Writes the map \a map to stream \a out.
1491 This function requires the key and value types to implement \c
1494 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1497 /*! \fn QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
1500 Reads a map from stream \a in into \a map.
1502 This function requires the key and value types to implement \c
1505 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1508 /*! \class QMultiMap
1509 \brief The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
1516 QMultiMap\<Key, T\> is one of Qt's generic \l{container classes}.
1517 It inherits QMap and extends it with a few convenience functions
1518 that make it more suitable than QMap for storing multi-valued
1519 maps. A multi-valued map is a map that allows multiple values
1520 with the same key; QMap normally doesn't allow that, unless you
1521 call QMap::insertMulti().
1523 Because QMultiMap inherits QMap, all of QMap's functionality also
1524 applies to QMultiMap. For example, you can use isEmpty() to test
1525 whether the map is empty, and you can traverse a QMultiMap using
1526 QMap's iterator classes (for example, QMapIterator). But in
1527 addition, it provides an insert() function that corresponds to
1528 QMap::insertMulti(), and a replace() function that corresponds to
1529 QMap::insert(). It also provides convenient operator+() and
1533 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 25
1535 Unlike QMap, QMultiMap provides no operator[]. Use value() or
1536 replace() if you want to access the most recently inserted item
1539 If you want to retrieve all the values for a single key, you can
1540 use values(const Key &key), which returns a QList<T>:
1542 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 26
1544 The items that share the same key are available from most
1545 recently to least recently inserted.
1547 If you prefer the STL-style iterators, you can call find() to get
1548 the iterator for the first item with a key and iterate from
1551 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 27
1553 QMultiMap's key and value data types must be \l{assignable data
1554 types}. This covers most data types you are likely to encounter,
1555 but the compiler won't let you, for example, store a QWidget as a
1556 value; instead, store a QWidget *. In addition, QMultiMap's key type
1557 must provide operator<(). See the QMap documentation for details.
1559 \sa QMap, QMapIterator, QMutableMapIterator, QMultiHash
1562 /*! \fn QMultiMap::QMultiMap()
1564 Constructs an empty map.
1567 /*! \fn QMultiMap::QMultiMap(const QMap<Key, T> &other)
1569 Constructs a copy of \a other (which can be a QMap or a
1575 /*! \fn QMultiMap::iterator QMultiMap::replace(const Key &key, const T &value)
1577 Inserts a new item with the key \a key and a value of \a value.
1579 If there is already an item with the key \a key, that item's value
1580 is replaced with \a value.
1582 If there are multiple items with the key \a key, the most
1583 recently inserted item's value is replaced with \a value.
1588 /*! \fn QMultiMap::iterator QMultiMap::insert(const Key &key, const T &value)
1590 Inserts a new item with the key \a key and a value of \a value.
1592 If there is already an item with the same key in the map, this
1593 function will simply create a new one. (This behavior is
1594 different from replace(), which overwrites the value of an
1600 /*! \fn QMultiMap &QMultiMap::operator+=(const QMultiMap &other)
1602 Inserts all the items in the \a other map into this map and
1603 returns a reference to this map.
1605 \sa insert(), operator+()
1608 /*! \fn QMultiMap QMultiMap::operator+(const QMultiMap &other) const
1610 Returns a map that contains all the items in this map in
1611 addition to all the items in \a other. If a key is common to both
1612 maps, the resulting map will contain the key multiple times.
1618 \fn bool QMultiMap::contains(const Key &key, const T &value) const
1621 Returns true if the map contains an item with key \a key and
1622 value \a value; otherwise returns false.
1624 \sa QMap::contains()
1628 \fn bool QMultiMap::contains(const Key &key) const
1630 \sa QMap::contains()
1634 \fn int QMultiMap::remove(const Key &key, const T &value)
1637 Removes all the items that have the key \a key and the value \a
1638 value from the map. Returns the number of items removed.
1644 \fn int QMultiMap::remove(const Key &key)
1650 \fn int QMultiMap::count(const Key &key, const T &value) const
1653 Returns the number of items with key \a key and value \a value.
1659 \fn int QMultiMap::count(const Key &key) const
1665 \fn int QMultiMap::count() const
1671 \fn typename QMap<Key, T>::iterator QMultiMap::find(const Key &key, const T &value)
1674 Returns an iterator pointing to the item with key \a key and
1675 value \a value in the map.
1677 If the map contains no such item, the function returns end().
1679 If the map contains multiple items with key \a key, this
1680 function returns an iterator that points to the most recently
1687 \fn typename QMap<Key, T>::iterator QMultiMap::find(const Key &key)
1693 \fn typename QMap<Key, T>::const_iterator QMultiMap::find(const Key &key, const T &value) const
1697 Returns a const iterator pointing to the item with the given \a key and
1698 \a value in the map.
1700 If the map contains no such item, the function returns end().
1702 If the map contains multiple items with the specified \a key, this
1703 function returns a const iterator that points to the most recently
1710 \fn typename QMap<Key, T>::const_iterator QMultiMap::find(const Key &key) const
1717 \fn typename QMap<Key, T>::const_iterator QMultiMap::constFind(const Key &key, const T &value) const
1720 Returns an iterator pointing to the item with key \a key and the
1721 value \a value in the map.
1723 If the map contains no such item, the function returns
1726 \sa QMap::constFind()
1730 \fn typename QMap<Key, T>::const_iterator QMultiMap::constFind(const Key &key) const
1732 \sa QMap::constFind()