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

Last change on this file since 659 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

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