source: trunk/src/corelib/tools/qmap.cpp@ 172

Last change on this file since 172 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 43.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qmap.h"
43
44#include <stdlib.h>
45
46#ifdef QT_QMAP_DEBUG
47# include <qstring.h>
48# include <qvector.h>
49#endif
50
51QT_BEGIN_NAMESPACE
52
53QMapData QMapData::shared_null = {
54 &shared_null,
55 { &shared_null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
56 Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, 0, false, true
57};
58
59QMapData *QMapData::createData()
60{
61 QMapData *d = new QMapData;
62 Node *e = reinterpret_cast<Node *>(d);
63 e->backward = e;
64 e->forward[0] = e;
65 d->ref = 1;
66 d->topLevel = 0;
67 d->size = 0;
68 d->randomBits = 0;
69 d->insertInOrder = false;
70 d->sharable = true;
71 return d;
72}
73
74void QMapData::continueFreeData(int offset)
75{
76 Node *e = reinterpret_cast<Node *>(this);
77 Node *cur = e->forward[0];
78 Node *prev;
79 while (cur != e) {
80 prev = cur;
81 cur = cur->forward[0];
82 qFree(reinterpret_cast<char *>(prev) - offset);
83 }
84 delete this;
85}
86
87QMapData::Node *QMapData::node_create(Node *update[], int offset)
88{
89 int level = 0;
90 uint mask = (1 << Sparseness) - 1;
91
92 while ((randomBits & mask) == mask && level < LastLevel) {
93 ++level;
94 mask <<= Sparseness;
95 }
96
97 ++randomBits;
98 if (level == 3 && !insertInOrder)
99 randomBits = qrand();
100
101 if (level > topLevel) {
102 Node *e = reinterpret_cast<Node *>(this);
103 level = ++topLevel;
104 e->forward[level] = e;
105 update[level] = e;
106 }
107
108 void *concreteNode = qMalloc(offset + sizeof(Node) + level * sizeof(Node *));
109 Node *abstractNode = reinterpret_cast<Node *>(reinterpret_cast<char *>(concreteNode) + offset);
110
111 abstractNode->backward = update[0];
112 update[0]->forward[0]->backward = abstractNode;
113
114 for (int i = level; i >= 0; i--) {
115 abstractNode->forward[i] = update[i]->forward[i];
116 update[i]->forward[i] = abstractNode;
117 update[i] = abstractNode;
118 }
119 ++size;
120 return abstractNode;
121}
122
123void QMapData::node_delete(Node *update[], int offset, Node *node)
124{
125 node->forward[0]->backward = node->backward;
126
127 for (int i = 0; i <= topLevel; ++i) {
128 if (update[i]->forward[i] != node)
129 break;
130 update[i]->forward[i] = node->forward[i];
131 }
132 --size;
133 qFree(reinterpret_cast<char *>(node) - offset);
134}
135
136#ifdef QT_QMAP_DEBUG
137
138uint QMapData::adjust_ptr(Node *node)
139{
140 if (node == reinterpret_cast<Node *>(this)) {
141 return (uint)0xDEADBEEF;
142 } else {
143 return (uint)node;
144 }
145}
146
147void QMapData::dump()
148{
149 qDebug("Map data (ref = %d, size = %d, randomBits = %#.8x)", ref.atomic, size, randomBits);
150
151 QString preOutput;
152 QVector<QString> output(topLevel + 1);
153 Node *e = reinterpret_cast<Node *>(this);
154
155 QString str;
156 str.sprintf(" %.8x", adjust_ptr(reinterpret_cast<Node *>(this)));
157 preOutput += str;
158
159 Node *update[LastLevel + 1];
160 for (int i = 0; i <= topLevel; ++i) {
161 str.sprintf("%d: [%.8x] -", i, adjust_ptr(forward[i]));
162 output[i] += str;
163 update[i] = forward[i];
164 }
165
166 Node *node = forward[0];
167 while (node != e) {
168 int level = 0;
169 while (level < topLevel && update[level + 1] == node)
170 ++level;
171
172 str.sprintf(" %.8x", adjust_ptr(node));
173 preOutput += str;
174
175 for (int i = 0; i <= level; ++i) {
176 str.sprintf("-> [%.8x] -", adjust_ptr(node->forward[i]));
177 output[i] += str;
178 update[i] = node->forward[i];
179 }
180 for (int j = level + 1; j <= topLevel; ++j)
181 output[j] += "---------------";
182 node = node->forward[0];
183 }
184
185 qDebug(preOutput.ascii());
186 for (int i = 0; i <= topLevel; ++i)
187 qDebug(output[i].ascii());
188}
189#endif
190
191/*!
192 \class QMap
193 \brief The QMap class is a template class that provides a skip-list-based dictionary.
194
195 \ingroup tools
196 \ingroup shared
197 \mainclass
198 \reentrant
199
200 QMap\<Key, T\> is one of Qt's generic \l{container classes}. It
201 stores (key, value) pairs and provides fast lookup of the
202 value associated with a key.
203
204 QMap and QHash provide very similar functionality. The
205 differences are:
206
207 \list
208 \i QHash provides faster lookups than QMap. (See \l{Algorithmic
209 Complexity} for details.)
210 \i When iterating over a QHash, the items are arbitrarily ordered.
211 With QMap, the items are always sorted by key.
212 \i The key type of a QHash must provide operator==() and a global
213 qHash(Key) function. The key type of a QMap must provide
214 operator<() specifying a total order.
215 \endlist
216
217 Here's an example QMap with QString keys and \c int values:
218 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 0
219
220 To insert a (key, value) pair into the map, you can use operator[]():
221
222 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 1
223
224 This inserts the following three (key, value) pairs into the
225 QMap: ("one", 1), ("three", 3), and ("seven", 7). Another way to
226 insert items into the map is to use insert():
227
228 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 2
229
230 To look up a value, use operator[]() or value():
231
232 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 3
233
234 If there is no item with the specified key in the map, these
235 functions return a \l{default-constructed value}.
236
237 If you want to check whether the map contains a certain key, use
238 contains():
239
240 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 4
241
242 There is also a value() overload that uses its second argument as
243 a default value if there is no item with the specified key:
244
245 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 5
246
247 In general, we recommend that you use contains() and value()
248 rather than operator[]() for looking up a key in a map. The
249 reason is that operator[]() silently inserts an item into the
250 map if no item exists with the same key (unless the map is
251 const). For example, the following code snippet will create 1000
252 items in memory:
253
254 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 6
255
256 To avoid this problem, replace \c map[i] with \c map.value(i)
257 in the code above.
258
259 If you want to navigate through all the (key, value) pairs stored
260 in a QMap, you can use an iterator. QMap provides both
261 \l{Java-style iterators} (QMapIterator and QMutableMapIterator)
262 and \l{STL-style iterators} (QMap::const_iterator and
263 QMap::iterator). Here's how to iterate over a QMap<QString, int>
264 using a Java-style iterator:
265
266 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 7
267
268 Here's the same code, but using an STL-style iterator this time:
269
270 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 8
271
272 The items are traversed in ascending key order.
273
274 Normally, a QMap allows only one value per key. If you call
275 insert() with a key that already exists in the QMap, the
276 previous value will be erased. For example:
277
278 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 9
279
280 However, you can store multiple values per key by using
281 insertMulti() instead of insert() (or using the convenience
282 subclass QMultiMap). If you want to retrieve all the values for a
283 single key, you can use values(const Key &key), which returns a
284 QList<T>:
285
286 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 10
287
288 The items that share the same key are available from most
289 recently to least recently inserted. Another approach is to call
290 find() to get the STL-style iterator for the first item with a
291 key and iterate from there:
292
293 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 11
294
295 If you only need to extract the values from a map (not the keys),
296 you can also use \l{foreach}:
297
298 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 12
299
300 Items can be removed from the map in several ways. One way is to
301 call remove(); this will remove any item with the given key.
302 Another way is to use QMutableMapIterator::remove(). In addition,
303 you can clear the entire map using clear().
304
305 QMap's key and value data types must be \l{assignable data
306 types}. This covers most data types you are likely to encounter,
307 but the compiler won't let you, for example, store a QWidget as a
308 value; instead, store a QWidget *. In addition, QMap's key type
309 must provide operator<(). QMap uses it to keep its items sorted,
310 and assumes that two keys \c x and \c y are equal if neither \c{x
311 < y} nor \c{y < x} is true.
312
313 Example:
314 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 13
315
316 In the example, we start by comparing the employees' names. If
317 they're equal, we compare their dates of birth to break the tie.
318
319 \sa QMapIterator, QMutableMapIterator, QHash, QSet
320*/
321
322/*! \fn QMap::QMap()
323
324 Constructs an empty map.
325
326 \sa clear()
327*/
328
329/*! \fn QMap::QMap(const QMap<Key, T> &other)
330
331 Constructs a copy of \a other.
332
333 This operation occurs in \l{constant time}, because QMap is
334 \l{implicitly shared}. This makes returning a QMap from a
335 function very fast. If a shared instance is modified, it will be
336 copied (copy-on-write), and this takes \l{linear time}.
337
338 \sa operator=()
339*/
340
341/*! \fn QMap::QMap(const std::map<Key, T> & other)
342
343 Constructs a copy of \a other.
344
345 This function is only available if Qt is configured with STL
346 compatibility enabled.
347
348 \sa toStdMap()
349*/
350
351/*! \fn std::map<Key, T> QMap::toStdMap() const
352
353 Returns an STL map equivalent to this QMap.
354
355 This function is only available if Qt is configured with STL
356 compatibility enabled.
357*/
358
359/*! \fn QMap::~QMap()
360
361 Destroys the map. References to the values in the map, and all
362 iterators over this map, become invalid.
363*/
364
365/*! \fn QMap<Key, T> &QMap::operator=(const QMap<Key, T> &other)
366
367 Assigns \a other to this map and returns a reference to this map.
368*/
369
370/*! \fn bool QMap::operator==(const QMap<Key, T> &other) const
371
372 Returns true if \a other is equal to this map; otherwise returns
373 false.
374
375 Two maps are considered equal if they contain the same (key,
376 value) pairs.
377
378 This function requires the value type to implement \c
379 operator==().
380
381 \sa operator!=()
382*/
383
384/*! \fn bool QMap::operator!=(const QMap<Key, T> &other) const
385
386 Returns true if \a other is not equal to this map; otherwise
387 returns false.
388
389 Two maps are considered equal if they contain the same (key,
390 value) pairs.
391
392 This function requires the value type to implement \c
393 operator==().
394
395 \sa operator==()
396*/
397
398/*! \fn int QMap::size() const
399
400 Returns the number of (key, value) pairs in the map.
401
402 \sa isEmpty(), count()
403*/
404
405/*!
406 \fn bool QMap::isEmpty() const
407
408 Returns true if the map contains no items; otherwise returns
409 false.
410
411 \sa size()
412*/
413
414/*! \fn void QMap::detach()
415
416 \internal
417
418 Detaches this map from any other maps with which it may share
419 data.
420
421 \sa isDetached()
422*/
423
424/*! \fn bool QMap::isDetached() const
425
426 \internal
427
428 Returns true if the map's internal data isn't shared with any
429 other map object; otherwise returns false.
430
431 \sa detach()
432*/
433
434/*! \fn void QMap::setSharable(bool sharable)
435
436 \internal
437*/
438
439/*! \fn void QMap::setInsertInOrder(bool sharable)
440
441 \internal
442*/
443
444/*! \fn void QMap::clear()
445
446 Removes all items from the map.
447
448 \sa remove()
449*/
450
451/*! \fn int QMap::remove(const Key &key)
452
453 Removes all the items that have the key \a key from the map.
454 Returns the number of items removed which is usually 1 but will be
455 0 if the key isn't in the map, or \> 1 if insertMulti() has been
456 used with the \a key.
457
458 \sa clear(), take(), QMultiMap::remove()
459*/
460
461/*! \fn T QMap::take(const Key &key)
462
463 Removes the item with the key \a key from the map and returns
464 the value associated with it.
465
466 If the item does not exist in the map, the function simply
467 returns a \l{default-constructed value}. If there are multiple
468 items for \a key in the map, only the most recently inserted one
469 is removed and returned.
470
471 If you don't use the return value, remove() is more efficient.
472
473 \sa remove()
474*/
475
476/*! \fn bool QMap::contains(const Key &key) const
477
478 Returns true if the map contains an item with key \a key;
479 otherwise returns false.
480
481 \sa count(), QMultiMap::contains()
482*/
483
484/*! \fn const T QMap::value(const Key &key) const
485
486 Returns the value associated with the key \a key.
487
488 If the map contains no item with key \a key, the function
489 returns a \l{default-constructed value}. If there are multiple
490 items for \a key in the map, the value of the most recently
491 inserted one is returned.
492
493 \sa key(), values(), contains(), operator[]()
494*/
495
496/*! \fn const T QMap::value(const Key &key, const T &defaultValue) const
497
498 \overload
499
500 If the map contains no item with key \a key, the function returns
501 \a defaultValue.
502*/
503
504/*! \fn T &QMap::operator[](const Key &key)
505
506 Returns the value associated with the key \a key as a modifiable
507 reference.
508
509 If the map contains no item with key \a key, the function inserts
510 a \l{default-constructed value} into the map with key \a key, and
511 returns a reference to it. If the map contains multiple items
512 with key \a key, this function returns a reference to the most
513 recently inserted value.
514
515 \sa insert(), value()
516*/
517
518/*! \fn const T QMap::operator[](const Key &key) const
519
520 \overload
521
522 Same as value().
523*/
524
525/*! \fn QList<Key> QMap::uniqueKeys() const
526 \since 4.2
527
528 Returns a list containing all the keys in the map in ascending
529 order. Keys that occur multiple times in the map (because items
530 were inserted with insertMulti(), or unite() was used) occur only
531 once in the returned list.
532
533 \sa keys(), values()
534*/
535
536/*! \fn QList<Key> QMap::keys() const
537
538 Returns a list containing all the keys in the map in ascending
539 order. Keys that occur multiple times in the map (because items
540 were inserted with insertMulti(), or unite() was used) also
541 occur multiple times in the list.
542
543 To obtain a list of unique keys, where each key from the map only
544 occurs once, use uniqueKeys().
545
546 The order is guaranteed to be the same as that used by values().
547
548 \sa uniqueKeys(), values(), key()
549*/
550
551/*! \fn QList<Key> QMap::keys(const T &value) const
552
553 \overload
554
555 Returns a list containing all the keys associated with value \a
556 value in ascending order.
557
558 This function can be slow (\l{linear time}), because QMap's
559 internal data structure is optimized for fast lookup by key, not
560 by value.
561*/
562
563/*! \fn Key QMap::key(const T &value) const
564
565 Returns the first key with value \a value.
566
567 If the map contains no item with value \a value, the function
568 returns a \link {default-constructed value} default-constructed
569 key \endlink.
570
571 This function can be slow (\l{linear time}), because QMap's
572 internal data structure is optimized for fast lookup by key, not
573 by value.
574
575 \sa value(), keys()
576*/
577
578/*!
579 \fn Key QMap::key(const T &value, const Key &defaultKey) const
580 \since 4.3
581 \overload
582
583 Returns the first key with value \a value, or \a defaultKey if
584 the map contains no item with value \a value.
585
586 This function can be slow (\l{linear time}), because QMap's
587 internal data structure is optimized for fast lookup by key, not
588 by value.
589*/
590
591/*! \fn QList<T> QMap::values() const
592
593 Returns a list containing all the values in the map, in ascending
594 order of their keys. If a key is associated with multiple values,
595 all of its values will be in the list, and not just the most
596 recently inserted one.
597
598 \sa keys(), value()
599*/
600
601/*! \fn QList<T> QMap::values(const Key &key) const
602
603 \overload
604
605 Returns a list containing all the values associated with key
606 \a key, from the most recently inserted to the least recently
607 inserted one.
608
609 \sa count(), insertMulti()
610*/
611
612/*! \fn int QMap::count(const Key &key) const
613
614 Returns the number of items associated with key \a key.
615
616 \sa contains(), insertMulti(), QMultiMap::count()
617*/
618
619/*! \fn int QMap::count() const
620
621 \overload
622
623 Same as size().
624*/
625
626/*! \fn QMap::iterator QMap::begin()
627
628 Returns an \l{STL-style iterator} pointing to the first item in
629 the map.
630
631 \sa constBegin(), end()
632*/
633
634/*! \fn QMap::const_iterator QMap::begin() const
635
636 \overload
637*/
638
639/*! \fn QMap::const_iterator QMap::constBegin() const
640
641 Returns a const \l{STL-style iterator} pointing to the first item
642 in the map.
643
644 \sa begin(), constEnd()
645*/
646
647/*! \fn QMap::iterator QMap::end()
648
649 Returns an \l{STL-style iterator} pointing to the imaginary item
650 after the last item in the map.
651
652 \sa begin(), constEnd()
653*/
654
655/*! \fn QMap::const_iterator QMap::end() const
656
657 \overload
658*/
659
660/*! \fn QMap::const_iterator QMap::constEnd() const
661
662 Returns a const \l{STL-style iterator} pointing to the imaginary
663 item after the last item in the map.
664
665 \sa constBegin(), end()
666*/
667
668/*! \fn QMap::iterator QMap::erase(iterator pos)
669
670 Removes the (key, value) pair pointed to by the iterator \a pos
671 from the map, and returns an iterator to the next item in the
672 map.
673
674 \sa remove()
675*/
676
677/*! \fn QMap::iterator QMap::find(const Key &key)
678
679 Returns an iterator pointing to the item with key \a key in the
680 map.
681
682 If the map contains no item with key \a key, the function
683 returns end().
684
685 If the map contains multiple items with key \a key, this
686 function returns an iterator that points to the most recently
687 inserted value. The other values are accessible by incrementing
688 the iterator. For example, here's some code that iterates over all
689 the items with the same key:
690
691 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 14
692
693 \sa constFind(), value(), values(), lowerBound(), upperBound(), QMultiMap::find()
694*/
695
696/*! \fn QMap::const_iterator QMap::find(const Key &key) const
697
698 \overload
699*/
700
701/*! \fn QMap::iterator QMap::constFind(const Key &key) const
702 \since 4.1
703
704 Returns an const iterator pointing to the item with key \a key in the
705 map.
706
707 If the map contains no item with key \a key, the function
708 returns constEnd().
709
710 \sa find(), QMultiMap::constFind()
711*/
712
713/*! \fn QMap::iterator QMap::lowerBound(const Key &key)
714
715 Returns an iterator pointing to the first item with key \a key in
716 the map. If the map contains no item with key \a key, the
717 function returns an iterator to the nearest item with a greater
718 key.
719
720 Example:
721 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 15
722
723 If the map contains multiple items with key \a key, this
724 function returns an iterator that points to the most recently
725 inserted value. The other values are accessible by incrementing
726 the iterator. For example, here's some code that iterates over all
727 the items with the same key:
728
729 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 16
730
731 \sa qLowerBound(), upperBound(), find()
732*/
733
734/*! \fn QMap::const_iterator QMap::lowerBound(const Key &key) const
735
736 \overload
737*/
738
739/*! \fn QMap::iterator QMap::upperBound(const Key &key)
740
741 Returns an iterator pointing to the item that immediately follows
742 the last item with key \a key in the map. If the map contains no
743 item with key \a key, the function returns an iterator to the
744 nearest item with a greater key.
745
746 Example:
747 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 17
748
749 \sa qUpperBound(), lowerBound(), find()
750*/
751
752/*! \fn QMap::const_iterator QMap::upperBound(const Key &key) const
753
754 \overload
755*/
756
757/*! \fn QMap::iterator QMap::insert(const Key &key, const T &value)
758
759 Inserts a new item with the key \a key and a value of \a value.
760
761 If there is already an item with the key \a key, that item's value
762 is replaced with \a value.
763
764 If there are multiple items with the key \a key, the most
765 recently inserted item's value is replaced with \a value.
766
767 \sa insertMulti()
768*/
769
770/*! \fn QMap::iterator QMap::insertMulti(const Key &key, const T &value)
771
772 Inserts a new item with the key \a key and a value of \a value.
773
774 If there is already an item with the same key in the map, this
775 function will simply create a new one. (This behavior is
776 different from insert(), which overwrites the value of an
777 existing item.)
778
779 \sa insert(), values()
780*/
781
782/*! \fn QMap<Key, T> &QMap::unite(const QMap<Key, T> &other)
783
784 Inserts all the items in the \a other map into this map. If a
785 key is common to both maps, the resulting map will contain the
786 key multiple times.
787
788 \sa insertMulti()
789*/
790
791/*! \typedef QMap::Iterator
792
793 Qt-style synonym for QMap::iterator.
794*/
795
796/*! \typedef QMap::ConstIterator
797
798 Qt-style synonym for QMap::const_iterator.
799*/
800
801/*! \typedef QMap::difference_type
802
803 Typedef for ptrdiff_t. Provided for STL compatibility.
804*/
805
806/*! \typedef QMap::key_type
807
808 Typedef for Key. Provided for STL compatibility.
809*/
810
811/*! \typedef QMap::mapped_type
812
813 Typedef for T. Provided for STL compatibility.
814*/
815
816/*! \typedef QMap::size_type
817
818 Typedef for int. Provided for STL compatibility.
819*/
820
821/*!
822 \fn bool QMap::empty() const
823
824 This function is provided for STL compatibility. It is equivalent
825 to isEmpty(), returning true if the map is empty; otherwise
826 returning false.
827*/
828
829/*! \class QMap::iterator
830 \brief The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
831
832 QMap features both \l{STL-style iterators} and \l{Java-style
833 iterators}. The STL-style iterators are more low-level and more
834 cumbersome to use; on the other hand, they are slightly faster
835 and, for developers who already know STL, have the advantage of
836 familiarity.
837
838 QMap\<Key, T\>::iterator allows you to iterate over a QMap (or
839 QMultiMap) and to modify the value (but not the key) stored under
840 a particular key. If you want to iterate over a const QMap, you
841 should use QMap::const_iterator. It is generally good practice to
842 use QMap::const_iterator on a non-const QMap as well, unless you
843 need to change the QMap through the iterator. Const iterators are
844 slightly faster, and can improve code readability.
845
846 The default QMap::iterator constructor creates an uninitialized
847 iterator. You must initialize it using a QMap function like
848 QMap::begin(), QMap::end(), or QMap::find() before you can
849 start iterating. Here's a typical loop that prints all the (key,
850 value) pairs stored in a map:
851
852 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 18
853
854 Unlike QHash, which stores its items in an arbitrary order, QMap
855 stores its items ordered by key. Items that share the same key
856 (because they were inserted using QMap::insertMulti(), or due to a
857 unite()) will appear consecutively, from the most recently to the
858 least recently inserted value.
859
860 Let's see a few examples of things we can do with a
861 QMap::iterator that we cannot do with a QMap::const_iterator.
862 Here's an example that increments every value stored in the QMap
863 by 2:
864
865 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 19
866
867 Here's an example that removes all the items whose key is a
868 string that starts with an underscore character:
869
870 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 20
871
872 The call to QMap::erase() removes the item pointed to by the
873 iterator from the map, and returns an iterator to the next item.
874 Here's another way of removing an item while iterating:
875
876 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 21
877
878 It might be tempting to write code like this:
879
880 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 22
881
882 However, this will potentially crash in \c{++i}, because \c i is
883 a dangling iterator after the call to erase().
884
885 Multiple iterators can be used on the same map. If you add items
886 to the map, existing iterators will remain valid. If you remove
887 items from the map, iterators that point to the removed items
888 will become dangling iterators.
889
890 \sa QMap::const_iterator, QMutableMapIterator
891*/
892
893/*! \fn QMap::iterator::operator QMapData::Node *() const
894
895 \internal
896*/
897
898/*! \typedef QMap::iterator::difference_type
899
900 \internal
901*/
902
903/*! \typedef QMap::iterator::iterator_category
904
905 \internal
906*/
907
908/*! \typedef QMap::iterator::pointer
909
910 \internal
911*/
912
913/*! \typedef QMap::iterator::reference
914
915 \internal
916*/
917
918/*! \typedef QMap::iterator::value_type
919
920 \internal
921*/
922
923/*! \fn QMap::iterator::iterator()
924
925 Constructs an uninitialized iterator.
926
927 Functions like key(), value(), and operator++() must not be
928 called on an uninitialized iterator. Use operator=() to assign a
929 value to it before using it.
930
931 \sa QMap::begin() QMap::end()
932*/
933
934/*! \fn QMap::iterator::iterator(QMapData::Node *node)
935
936 \internal
937*/
938
939/*! \fn const Key &QMap::iterator::key() const
940
941 Returns the current item's key as a const reference.
942
943 There is no direct way of changing an item's key through an
944 iterator, although it can be done by calling QMap::erase()
945 followed by QMap::insert() or QMap::insertMulti().
946
947 \sa value()
948*/
949
950/*! \fn T &QMap::iterator::value() const
951
952 Returns a modifiable reference to the current item's value.
953
954 You can change the value of an item by using value() on
955 the left side of an assignment, for example:
956
957 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 23
958
959 \sa key(), operator*()
960*/
961
962/*! \fn T &QMap::iterator::operator*() const
963
964 Returns a modifiable reference to the current item's value.
965
966 Same as value().
967
968 \sa key()
969*/
970
971/*! \fn T *QMap::iterator::operator->() const
972
973 Returns a pointer to the current item's value.
974
975 \sa value()
976*/
977
978/*!
979 \fn bool QMap::iterator::operator==(const iterator &other) const
980 \fn bool QMap::iterator::operator==(const const_iterator &other) const
981
982 Returns true if \a other points to the same item as this
983 iterator; otherwise returns false.
984
985 \sa operator!=()
986*/
987
988/*!
989 \fn bool QMap::iterator::operator!=(const iterator &other) const
990 \fn bool QMap::iterator::operator!=(const const_iterator &other) const
991
992 Returns true if \a other points to a different item than this
993 iterator; otherwise returns false.
994
995 \sa operator==()
996*/
997
998/*! \fn QMap::iterator QMap::iterator::operator++()
999
1000 The prefix ++ operator (\c{++i}) advances the iterator to the
1001 next item in the map and returns an iterator to the new current
1002 item.
1003
1004 Calling this function on QMap::end() leads to undefined results.
1005
1006 \sa operator--()
1007*/
1008
1009/*! \fn QMap::iterator QMap::iterator::operator++(int)
1010
1011 \overload
1012
1013 The postfix ++ operator (\c{i++}) advances the iterator to the
1014 next item in the map and returns an iterator to the previously
1015 current item.
1016*/
1017
1018/*! \fn QMap::iterator QMap::iterator::operator--()
1019
1020 The prefix -- operator (\c{--i}) makes the preceding item
1021 current and returns an iterator pointing to the new current item.
1022
1023 Calling this function on QMap::begin() leads to undefined
1024 results.
1025
1026 \sa operator++()
1027*/
1028
1029/*! \fn QMap::iterator QMap::iterator::operator--(int)
1030
1031 \overload
1032
1033 The prefix -- operator (\c{--i}) makes the preceding item
1034 current and returns an iterator pointing to the previously
1035 current item.
1036*/
1037
1038/*! \fn QMap::iterator QMap::iterator::operator+(int j) const
1039
1040 Returns an iterator to the item at \a j positions forward from
1041 this iterator. (If \a j is negative, the iterator goes backward.)
1042
1043 This operation can be slow for large \a j values.
1044
1045 \sa operator-()
1046
1047*/
1048
1049/*! \fn QMap::iterator QMap::iterator::operator-(int j) const
1050
1051 Returns an iterator to the item at \a j positions backward from
1052 this iterator. (If \a j is negative, the iterator goes forward.)
1053
1054 This operation can be slow for large \a j values.
1055
1056 \sa operator+()
1057*/
1058
1059/*! \fn QMap::iterator &QMap::iterator::operator+=(int j)
1060
1061 Advances the iterator by \a j items. (If \a j is negative, the
1062 iterator goes backward.)
1063
1064 \sa operator-=(), operator+()
1065*/
1066
1067/*! \fn QMap::iterator &QMap::iterator::operator-=(int j)
1068
1069 Makes the iterator go back by \a j items. (If \a j is negative,
1070 the iterator goes forward.)
1071
1072 \sa operator+=(), operator-()
1073*/
1074
1075/*! \class QMap::const_iterator
1076 \brief The QMap::const_iterator class provides an STL-style const iterator for QMap and QMultiMap.
1077
1078 QMap features both \l{STL-style iterators} and \l{Java-style
1079 iterators}. The STL-style iterators are more low-level and more
1080 cumbersome to use; on the other hand, they are slightly faster
1081 and, for developers who already know STL, have the advantage of
1082 familiarity.
1083
1084 QMap\<Key, T\>::const_iterator allows you to iterate over a QMap
1085 (or a QMultiMap). If you want to modify the QMap as you iterate
1086 over it, you must use QMap::iterator instead. It is generally
1087 good practice to use QMap::const_iterator on a non-const QMap as
1088 well, unless you need to change the QMap through the iterator.
1089 Const iterators are slightly faster, and can improve code
1090 readability.
1091
1092 The default QMap::const_iterator constructor creates an
1093 uninitialized iterator. You must initialize it using a QMap
1094 function like QMap::constBegin(), QMap::constEnd(), or
1095 QMap::find() before you can start iterating. Here's a typical
1096 loop that prints all the (key, value) pairs stored in a map:
1097
1098 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 24
1099
1100 Unlike QHash, which stores its items in an arbitrary order, QMap
1101 stores its items ordered by key. Items that share the same key
1102 (because they were inserted using QMap::insertMulti()) will
1103 appear consecutively, from the most recently to the least
1104 recently inserted value.
1105
1106 Multiple iterators can be used on the same map. If you add items
1107 to the map, existing iterators will remain valid. If you remove
1108 items from the map, iterators that point to the removed items
1109 will become dangling iterators.
1110
1111 \sa QMap::iterator, QMapIterator
1112*/
1113
1114/*! \fn QMap::const_iterator::operator QMapData::Node *() const
1115
1116 \internal
1117*/
1118
1119/*! \typedef QMap::const_iterator::difference_type
1120
1121 \internal
1122*/
1123
1124/*! \typedef QMap::const_iterator::iterator_category
1125
1126 \internal
1127*/
1128
1129/*! \typedef QMap::const_iterator::pointer
1130
1131 \internal
1132*/
1133
1134/*! \typedef QMap::const_iterator::reference
1135
1136 \internal
1137*/
1138
1139/*! \typedef QMap::const_iterator::value_type
1140
1141 \internal
1142*/
1143
1144/*! \fn QMap::const_iterator::const_iterator()
1145
1146 Constructs an uninitialized iterator.
1147
1148 Functions like key(), value(), and operator++() must not be
1149 called on an uninitialized iterator. Use operator=() to assign a
1150 value to it before using it.
1151
1152 \sa QMap::constBegin() QMap::constEnd()
1153*/
1154
1155/*! \fn QMap::const_iterator::const_iterator(QMapData::Node *node)
1156
1157 \internal
1158*/
1159
1160/*! \fn QMap::const_iterator::const_iterator(const iterator &other)
1161
1162 Constructs a copy of \a other.
1163*/
1164
1165/*! \fn const Key &QMap::const_iterator::key() const
1166
1167 Returns the current item's key.
1168
1169 \sa value()
1170*/
1171
1172/*! \fn const T &QMap::const_iterator::value() const
1173
1174 Returns the current item's value.
1175
1176 \sa key(), operator*()
1177*/
1178
1179/*! \fn const T &QMap::const_iterator::operator*() const
1180
1181 Returns the current item's value.
1182
1183 Same as value().
1184
1185 \sa key()
1186*/
1187
1188/*! \fn const T *QMap::const_iterator::operator->() const
1189
1190 Returns a pointer to the current item's value.
1191
1192 \sa value()
1193*/
1194
1195/*! \fn bool QMap::const_iterator::operator==(const const_iterator &other) const
1196
1197 Returns true if \a other points to the same item as this
1198 iterator; otherwise returns false.
1199
1200 \sa operator!=()
1201*/
1202
1203/*! \fn bool QMap::const_iterator::operator!=(const const_iterator &other) const
1204
1205 Returns true if \a other points to a different item than this
1206 iterator; otherwise returns false.
1207
1208 \sa operator==()
1209*/
1210
1211/*! \fn QMap::const_iterator QMap::const_iterator::operator++()
1212
1213 The prefix ++ operator (\c{++i}) advances the iterator to the
1214 next item in the map and returns an iterator to the new current
1215 item.
1216
1217 Calling this function on QMap::end() leads to undefined results.
1218
1219 \sa operator--()
1220*/
1221
1222/*! \fn QMap::const_iterator QMap::const_iterator::operator++(int)
1223
1224 \overload
1225
1226 The postfix ++ operator (\c{i++}) advances the iterator to the
1227 next item in the map and returns an iterator to the previously
1228 current item.
1229*/
1230
1231/*! \fn QMap::const_iterator &QMap::const_iterator::operator--()
1232
1233 The prefix -- operator (\c{--i}) makes the preceding item
1234 current and returns an iterator pointing to the new current item.
1235
1236 Calling this function on QMap::begin() leads to undefined
1237 results.
1238
1239 \sa operator++()
1240*/
1241
1242/*! \fn QMap::const_iterator QMap::const_iterator::operator--(int)
1243
1244 \overload
1245
1246 The postfix -- operator (\c{i--}) makes the preceding item
1247 current and returns an iterator pointing to the previously
1248 current item.
1249*/
1250
1251/*! \fn QMap::const_iterator QMap::const_iterator::operator+(int j) const
1252
1253 Returns an iterator to the item at \a j positions forward from
1254 this iterator. (If \a j is negative, the iterator goes backward.)
1255
1256 This operation can be slow for large \a j values.
1257
1258 \sa operator-()
1259*/
1260
1261/*! \fn QMap::const_iterator QMap::const_iterator::operator-(int j) const
1262
1263 Returns an iterator to the item at \a j positions backward from
1264 this iterator. (If \a j is negative, the iterator goes forward.)
1265
1266 This operation can be slow for large \a j values.
1267
1268 \sa operator+()
1269*/
1270
1271/*! \fn QMap::const_iterator &QMap::const_iterator::operator+=(int j)
1272
1273 Advances the iterator by \a j items. (If \a j is negative, the
1274 iterator goes backward.)
1275
1276 This operation can be slow for large \a j values.
1277
1278 \sa operator-=(), operator+()
1279*/
1280
1281/*! \fn QMap::const_iterator &QMap::const_iterator::operator-=(int j)
1282
1283 Makes the iterator go back by \a j items. (If \a j is negative,
1284 the iterator goes forward.)
1285
1286 This operation can be slow for large \a j values.
1287
1288 \sa operator+=(), operator-()
1289*/
1290
1291/*! \fn QDataStream &operator<<(QDataStream &out, const QMap<Key, T> &map)
1292 \relates QMap
1293
1294 Writes the map \a map to stream \a out.
1295
1296 This function requires the key and value types to implement \c
1297 operator<<().
1298
1299 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1300*/
1301
1302/*! \fn QDataStream &operator>>(QDataStream &in, QMap<Key, T> &map)
1303 \relates QMap
1304
1305 Reads a map from stream \a in into \a map.
1306
1307 This function requires the key and value types to implement \c
1308 operator>>().
1309
1310 \sa \link datastreamformat.html Format of the QDataStream operators \endlink
1311*/
1312
1313/*! \class QMultiMap
1314 \brief The QMultiMap class is a convenience QMap subclass that provides multi-valued maps.
1315
1316 \ingroup tools
1317 \ingroup shared
1318 \mainclass
1319 \reentrant
1320
1321 QMultiMap\<Key, T\> is one of Qt's generic \l{container classes}.
1322 It inherits QMap and extends it with a few convenience functions
1323 that make it more suitable than QMap for storing multi-valued
1324 maps. A multi-valued map is a map that allows multiple values
1325 with the same key; QMap normally doesn't allow that, unless you
1326 call QMap::insertMulti().
1327
1328 Because QMultiMap inherits QMap, all of QMap's functionality also
1329 applies to QMultiMap. For example, you can use isEmpty() to test
1330 whether the map is empty, and you can traverse a QMultiMap using
1331 QMap's iterator classes (for example, QMapIterator). But in
1332 addition, it provides an insert() function that corresponds to
1333 QMap::insertMulti(), and a replace() function that corresponds to
1334 QMap::insert(). It also provides convenient operator+() and
1335 operator+=().
1336
1337 Example:
1338 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 25
1339
1340 Unlike QMap, QMultiMap provides no operator[]. Use value() or
1341 replace() if you want to access the most recently inserted item
1342 with a certain key.
1343
1344 If you want to retrieve all the values for a single key, you can
1345 use values(const Key &key), which returns a QList<T>:
1346
1347 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 26
1348
1349 The items that share the same key are available from most
1350 recently to least recently inserted.
1351
1352 If you prefer the STL-style iterators, you can call find() to get
1353 the iterator for the first item with a key and iterate from
1354 there:
1355
1356 \snippet doc/src/snippets/code/src_corelib_tools_qmap.cpp 27
1357
1358 QMultiMap's key and value data types must be \l{assignable data
1359 types}. This covers most data types you are likely to encounter,
1360 but the compiler won't let you, for example, store a QWidget as a
1361 value; instead, store a QWidget *. In addition, QMultiMap's key type
1362 must provide operator<(). See the QMap documentation for details.
1363
1364 \sa QMap, QMapIterator, QMutableMapIterator, QMultiHash
1365*/
1366
1367/*! \fn QMultiMap::QMultiMap()
1368
1369 Constructs an empty map.
1370*/
1371
1372/*! \fn QMultiMap::QMultiMap(const QMap<Key, T> &other)
1373
1374 Constructs a copy of \a other (which can be a QMap or a
1375 QMultiMap).
1376
1377 \sa operator=()
1378*/
1379
1380/*! \fn QMultiMap::iterator QMultiMap::replace(const Key &key, const T &value)
1381
1382 Inserts a new item with the key \a key and a value of \a value.
1383
1384 If there is already an item with the key \a key, that item's value
1385 is replaced with \a value.
1386
1387 If there are multiple items with the key \a key, the most
1388 recently inserted item's value is replaced with \a value.
1389
1390 \sa insert()
1391*/
1392
1393/*! \fn QMultiMap::iterator QMultiMap::insert(const Key &key, const T &value)
1394
1395 Inserts a new item with the key \a key and a value of \a value.
1396
1397 If there is already an item with the same key in the map, this
1398 function will simply create a new one. (This behavior is
1399 different from replace(), which overwrites the value of an
1400 existing item.)
1401
1402 \sa replace()
1403*/
1404
1405/*! \fn QMultiMap &QMultiMap::operator+=(const QMultiMap &other)
1406
1407 Inserts all the items in the \a other map into this map and
1408 returns a reference to this map.
1409
1410 \sa insert(), operator+()
1411*/
1412
1413/*! \fn QMultiMap QMultiMap::operator+(const QMultiMap &other) const
1414
1415 Returns a map that contains all the items in this map in
1416 addition to all the items in \a other. If a key is common to both
1417 maps, the resulting map will contain the key multiple times.
1418
1419 \sa operator+=()
1420*/
1421
1422/*!
1423 \fn bool QMultiMap::contains(const Key &key, const T &value) const
1424 \since 4.3
1425
1426 Returns true if the map contains an item with key \a key and
1427 value \a value; otherwise returns false.
1428
1429 \sa QMap::contains()
1430*/
1431
1432/*!
1433 \fn bool QMultiMap::contains(const Key &key) const
1434 \overload
1435 \sa QMap::contains()
1436*/
1437
1438/*!
1439 \fn int QMultiMap::remove(const Key &key, const T &value)
1440 \since 4.3
1441
1442 Removes all the items that have the key \a key and the value \a
1443 value from the map. Returns the number of items removed.
1444
1445 \sa QMap::remove()
1446*/
1447
1448/*!
1449 \fn int QMultiMap::remove(const Key &key)
1450 \overload
1451 \sa QMap::remove()
1452*/
1453
1454/*!
1455 \fn int QMultiMap::count(const Key &key, const T &value) const
1456 \since 4.3
1457
1458 Returns the number of items with key \a key and value \a value.
1459
1460 \sa QMap::count()
1461*/
1462
1463/*!
1464 \fn int QMultiMap::count(const Key &key) const
1465 \overload
1466 \sa QMap::count()
1467*/
1468
1469/*!
1470 \fn int QMultiMap::count() const
1471 \overload
1472 \sa QMap::count()
1473*/
1474
1475/*!
1476 \fn typename QMap<Key, T>::iterator QMultiMap::find(const Key &key, const T &value)
1477 \since 4.3
1478
1479 Returns an iterator pointing to the item with key \a key and
1480 value \a value in the map.
1481
1482 If the map contains no such item, the function returns end().
1483
1484 If the map contains multiple items with key \a key, this
1485 function returns an iterator that points to the most recently
1486 inserted value.
1487
1488 \sa QMap::find()
1489*/
1490
1491/*!
1492 \fn typename QMap<Key, T>::iterator QMultiMap::find(const Key &key)
1493 \overload
1494 \sa QMap::find()
1495*/
1496
1497/*!
1498 \fn typename QMap<Key, T>::const_iterator QMultiMap::find(const Key &key, const T &value) const
1499 \since 4.3
1500 \overload
1501
1502 Returns a const iterator pointing to the item with the given \a key and
1503 \a value in the map.
1504
1505 If the map contains no such item, the function returns end().
1506
1507 If the map contains multiple items with the specified \a key, this
1508 function returns a const iterator that points to the most recently
1509 inserted value.
1510
1511 \sa QMap::find()
1512*/
1513
1514/*!
1515 \fn typename QMap<Key, T>::const_iterator QMultiMap::find(const Key &key) const
1516 \since 4.3
1517 \overload
1518 \sa QMap::find()
1519*/
1520
1521/*!
1522 \fn typename QMap<Key, T>::const_iterator QMultiMap::constFind(const Key &key, const T &value) const
1523 \since 4.3
1524
1525 Returns an iterator pointing to the item with key \a key and the
1526 value \a value in the map.
1527
1528 If the map contains no such item, the function returns
1529 constEnd().
1530
1531 \sa QMap::constFind()
1532*/
1533
1534/*!
1535 \fn typename QMap<Key, T>::const_iterator QMultiMap::constFind(const Key &key) const
1536 \overload
1537 \sa QMap::constFind()
1538*/
1539
1540/*!
1541 \fn T &QMap::iterator::data() const
1542
1543 Use value() instead.
1544*/
1545
1546/*!
1547 \fn const T &QMap::const_iterator::data() const
1548
1549 Use value() instead.
1550*/
1551
1552/*!
1553 \fn iterator QMap::remove(iterator it)
1554
1555 Use erase(\a it) instead.
1556*/
1557
1558/*!
1559 \fn void QMap::erase(const Key &key)
1560
1561 Use remove(\a key) instead.
1562*/
1563
1564/*!
1565 \fn iterator QMap::insert(const Key &key, const T &value, bool overwrite);
1566
1567 Use the two-argument insert() overload instead. If you don't want
1568 to overwrite, call contains() beforehand.
1569
1570 \oldcode
1571 QMap<QString, int> map;
1572 ...
1573 map.insert("delay", 30000, false);
1574 \newcode
1575 QMap<QString, int> map;
1576 ...
1577 if (!map.contains("delay"))
1578 map.insert("delay", 30000);
1579 \endcode
1580*/
1581
1582/*!
1583 \fn iterator QMap::replace(const Key &key, const T &value)
1584
1585 Use remove() then insert().
1586*/
1587
1588QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.