summaryrefslogtreecommitdiffstats
path: root/src/corelib/doc/snippets/code
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-07-22 14:07:48 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-06 19:15:39 +0200
commit14090760a87f23509b7bb5ad846537c766cb44a5 (patch)
tree1bdaa0b0ca86c17579aef1fa673344bc2008a40a /src/corelib/doc/snippets/code
parent7d27316d9fe736fd863dbd389571ee7906d6e559 (diff)
Long Live QMap as a refcounted std::map!
... and QMultiMap as std::multimap. Just use the implementation from the STL; we can't really claim that our code is much better than STL's, or does things any differently (de facto they're both red-black trees). Decouple QMultiMap from QMap, by making it NOT inherit from QMap any longer. This completes the deprecation started in 5.15: QMap now does not store duplicated keys any more. Something to establish is where to put the QExplictlySharedDataPointer replcement that is in there as an ad-hoc solution. There's a number of patches in-flight by Marc that try to introduce the same (or very similar) functionality. Miscellanea changes to the Q(Multi)Map code itself: * consistently use size_type instead of int; * pass iterators by value; * drop QT_STRICT_ITERATORS; * iterators implictly convert to const_iterators, and APIs take const_iterators; * iterators are just bidirectional and not random access; * added noexcept where it makes sense; * "inline" dropped (churn); * qMapLessThanKey dropped (undocumented, 0 hits in Qt, 1 hit in KDE); * operator== on Q(Multi)Map requires operator== on the key type (we're checking for equality, not equivalence!). Very few breakages occur in qtbase. [ChangeLog][Potentially Source-Incompatible Changes] QMap does not support multiple equivalent keys any more. Any related functionality has been removed from QMap, following the deprecation that happened in Qt 5.15. Use QMultiMap for this use case. [ChangeLog][Potentially Source-Incompatible Changes] QMap and QMultiMap iterators random-access API have been removed. Note that the iterators have always been just bidirectional; moving an iterator by N positions can still be achieved using std::next or std::advance, at the same cost as before (O(N)). [ChangeLog][Potentially Source-Incompatible Changes] QMultiMap does not inherit from QMap any more. Amongst other things, this means that iterators on a QMultiMap now belong to the QMultiMap class (and not to the QMap class); new Java iterators have been added. Change-Id: I5a0fe9b020f92c21b37065a1defff783b5d2b7a9 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Lars Knoll <lars.knoll@qt.io>
Diffstat (limited to 'src/corelib/doc/snippets/code')
-rw-r--r--src/corelib/doc/snippets/code/doc_src_qiterator.cpp69
-rw-r--r--src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp324
2 files changed, 393 insertions, 0 deletions
diff --git a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
index ed0cf9e329..2ee618d394 100644
--- a/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
+++ b/src/corelib/doc/snippets/code/doc_src_qiterator.cpp
@@ -183,6 +183,35 @@ while (i.findNext(widget)) {
}
//! [28]
+//! [26multi]
+QMultiMap<int, QWidget *> multimap;
+...
+QMultiMapIterator<int, QWidget *> i(multimap);
+while (i.hasNext()) {
+ i.next();
+ qDebug() << i.key() << ": " << i.value();
+}
+//! [26multi]
+
+
+//! [27multi]
+QMultiMapIterator<int, QWidget *> i(multimap);
+i.toBack();
+while (i.hasPrevious()) {
+ i.previous();
+ qDebug() << i.key() << ": " << i.value();
+}
+//! [27multi]
+
+
+//! [28multi]
+QMultiMapIterator<int, QWidget *> i(multimap);
+while (i.findNext(widget)) {
+ qDebug() << "Found widget " << widget << " under key "
+ << i.key();
+}
+//! [28multi]
+
//! [29]
QHash<int, QWidget *> hash;
@@ -244,6 +273,46 @@ while (i.hasNext()) {
//! [35]
+//! [32multi]
+QMultiMap<int, QWidget *> multimap;
+...
+QMutableMultiMapIterator<int, QWidget *> i(multimap);
+while (i.hasNext()) {
+ i.next();
+ qDebug() << i.key() << ": " << i.value();
+}
+//! [32multi]
+
+
+//! [33multi]
+QMutableMultiMapIterator<int, QWidget *> i(multimap);
+i.toBack();
+while (i.hasPrevious()) {
+ i.previous();
+ qDebug() << i.key() << ": " << i.value();
+}
+//! [33multi]
+
+
+//! [34multi]
+QMutableMultiMapIterator<int, QWidget *> i(multimap);
+while (i.findNext(widget)) {
+ qDebug() << "Found widget " << widget << " under key "
+ << i.key();
+}
+//! [34multi]
+
+
+//! [35multi]
+QMutableMultiMapIterator<QString, QString> i(multimap);
+while (i.hasNext()) {
+ i.next();
+ if (i.key() == i.value())
+ i.remove();
+}
+//! [35multi]
+
+
//! [36]
QHash<int, QWidget *> hash;
...
diff --git a/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp
new file mode 100644
index 0000000000..9c2a01834b
--- /dev/null
+++ b/src/corelib/doc/snippets/code/src_corelib_tools_qmultimap.cpp
@@ -0,0 +1,324 @@
+/****************************************************************************
+**
+** Copyright (C) 2020 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
+** Copyright (C) 2016 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the documentation of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:BSD$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** BSD License Usage
+** Alternatively, you may use this file under the terms of the BSD license
+** as follows:
+**
+** "Redistribution and use in source and binary forms, with or without
+** modification, are permitted provided that the following conditions are
+** met:
+** * Redistributions of source code must retain the above copyright
+** notice, this list of conditions and the following disclaimer.
+** * Redistributions in binary form must reproduce the above copyright
+** notice, this list of conditions and the following disclaimer in
+** the documentation and/or other materials provided with the
+** distribution.
+** * Neither the name of The Qt Company Ltd nor the names of its
+** contributors may be used to endorse or promote products derived
+** from this software without specific prior written permission.
+**
+**
+** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+//! [0]
+QMultiMap<QString, int> multimap;
+//! [0]
+
+
+//! [2]
+multimap.insert("a", 1);
+multimap.insert("b", 3);
+multimap.insert("c", 7);
+multimap.insert("c", -5);
+//! [2]
+
+
+//! [3]
+int num2 = multimap.value("a"); // 1
+int num3 = multimap.value("thirteen"); // not found; 0
+int num3 = 0;
+auto it = multimap.value("b");
+if (it != multimap.end()) {
+ num3 = it.value();
+}
+//! [3]
+
+
+//! [4]
+int timeout = 30;
+if (multimap.contains("TIMEOUT"))
+ timeout = multimap.value("TIMEOUT");
+
+// better:
+auto it = multimap.find("TIMEOUT");
+if (it != multimap.end())
+ timeout = it.value();
+//! [4]
+
+
+//! [5]
+int timeout = multimap.value("TIMEOUT", 30);
+//! [5]
+
+
+//! [7]
+QMultiMapIterator<QString, int> i(multimap);
+while (i.hasNext()) {
+ i.next();
+ cout << i.key() << ": " << i.value() << Qt::endl;
+}
+//! [7]
+
+
+//! [8]
+auto i = multimap.constBegin();
+while (i != multimap.constEnd()) {
+ cout << i.key() << ": " << i.value() << Qt::endl;
+ ++i;
+}
+//! [8]
+
+
+//! [9]
+multimap.insert("plenty", 100);
+multimap.insert("plenty", 2000);
+// multimap.size() == 2
+//! [9]
+
+
+//! [10]
+QList<int> values = multimap.values("plenty");
+for (int i = 0; i < values.size(); ++i)
+ cout << values.at(i) << Qt::endl;
+//! [10]
+
+
+//! [11]
+QMultiMap<QString, int>::iterator i = multimap.find("plenty");
+while (i != map.end() && i.key() == "plenty") {
+ cout << i.value() << Qt::endl;
+ ++i;
+}
+
+// better:
+auto [i, end] = multimap.equal_range("plenty");
+while (i != end) {
+ cout << i.value() << Qt::endl;
+ ++i;
+}
+//! [11]
+
+
+//! [12]
+QMap<QString, int> multimap;
+...
+foreach (int value, multimap)
+ cout << value << Qt::endl;
+//! [12]
+
+
+//! [13]
+#ifndef EMPLOYEE_H
+#define EMPLOYEE_H
+
+class Employee
+{
+public:
+ Employee() {}
+ Employee(const QString &name, QDate dateOfBirth);
+ ...
+
+private:
+ QString myName;
+ QDate myDateOfBirth;
+};
+
+inline bool operator<(const Employee &e1, const Employee &e2)
+{
+ if (e1.name() != e2.name())
+ return e1.name() < e2.name();
+ return e1.dateOfBirth() < e2.dateOfBirth();
+}
+
+#endif // EMPLOYEE_H
+//! [13]
+
+
+//! [15]
+QMultiMap<int, QString> multimap;
+multimap.insert(1, "one");
+multimap.insert(5, "five");
+multimap.insert(5, "five (2)");
+multimap.insert(10, "ten");
+
+multimap.lowerBound(0); // returns iterator to (1, "one")
+multimap.lowerBound(1); // returns iterator to (1, "one")
+multimap.lowerBound(2); // returns iterator to (5, "five")
+multimap.lowerBound(5); // returns iterator to (5, "five")
+multimap.lowerBound(6); // returns iterator to (10, "ten")
+multimap.lowerBound(10); // returns iterator to (10, "ten")
+multimap.lowerBound(999); // returns end()
+//! [15]
+
+
+//! [16]
+QMap<QString, int> multimap;
+...
+QMap<QString, int>::const_iterator i = multimap.lowerBound("HDR");
+QMap<QString, int>::const_iterator upperBound = multimap.upperBound("HDR");
+while (i != upperBound) {
+ cout << i.value() << Qt::endl;
+ ++i;
+}
+//! [16]
+
+
+//! [17]
+QMultiMap<int, QString> multimap;
+multimap.insert(1, "one");
+multimap.insert(5, "five");
+multimap.insert(5, "five (2)");
+multimap.insert(10, "ten");
+
+multimap.upperBound(0); // returns iterator to (1, "one")
+multimap.upperBound(1); // returns iterator to (5, "five")
+multimap.upperBound(2); // returns iterator to (5, "five")
+multimap.lowerBound(5); // returns iterator to (5, "five (2)")
+multimap.lowerBound(6); // returns iterator to (10, "ten")
+multimap.upperBound(10); // returns end()
+multimap.upperBound(999); // returns end()
+//! [17]
+
+
+//! [18]
+QMultiMap<QString, int> multimap;
+multimap.insert("January", 1);
+multimap.insert("February", 2);
+...
+multimap.insert("December", 12);
+
+QMap<QString, int>::iterator i;
+for (i = multimap.begin(); i != multimap.end(); ++i)
+ cout << i.key() << ": " << i.value() << Qt::endl;
+//! [18]
+
+
+//! [19]
+QMultiMap<QString, int>::iterator i;
+for (i = multimap.begin(); i != multimap.end(); ++i)
+ i.value() += 2;
+//! [19]
+
+
+//! [20]
+QMultiMap<QString, int>::iterator i = multimap.begin();
+while (i != multimap.end()) {
+ if (i.key().startsWith('_'))
+ i = multimap.erase(i);
+ else
+ ++i;
+}
+//! [20]
+
+
+//! [21]
+QMultiMap<QString, int>::iterator i = multimap.begin();
+while (i != multimap.end()) {
+ QMap<QString, int>::iterator prev = i;
+ ++i;
+ if (prev.key().startsWith('_'))
+ multimap.erase(prev);
+}
+//! [21]
+
+
+//! [22]
+// WRONG
+while (i != multimap.end()) {
+ if (i.key().startsWith('_'))
+ multimap.erase(i);
+ ++i;
+}
+//! [22]
+
+
+//! [23]
+if (i.key() == "Hello")
+ i.value() = "Bonjour";
+//! [23]
+
+
+//! [24]
+QMultiMap<QString, int> multi;
+multimap.insert("January", 1);
+multimap.insert("February", 2);
+...
+multimap.insert("December", 12);
+
+QMultiMap<QString, int>::const_iterator i;
+for (i = multimap.constBegin(); i != multimap.constEnd(); ++i)
+ cout << i.key() << ": " << i.value() << Qt::endl;
+//! [24]
+
+
+//! [25]
+QMultiMap<QString, int> map1, map2, map3;
+
+map1.insert("plenty", 100);
+map1.insert("plenty", 2000);
+// map1.size() == 2
+
+map2.insert("plenty", 5000);
+// map2.size() == 1
+
+map3 = map1 + map2;
+// map3.size() == 3
+//! [25]
+
+//! [keyiterator1]
+for (QMultiMap<int, QString>::const_iterator it = multimap.cbegin(), end = multimap.cend(); it != end; ++it) {
+ cout << "The key: " << it.key() << Qt::endl
+ cout << "The value: " << it.value() << Qt::endl;
+ cout << "Also the value: " << (*it) << Qt::endl;
+}
+//! [keyiterator1]
+
+//! [keyiterator2]
+// Inefficient, keys() is expensive
+QList<int> keys = multimap.keys();
+int numPrimes = std::count_if(multimap.cbegin(), multimap.cend(), isPrimeNumber);
+qDeleteAll(multimap2.keys());
+
+// Efficient, no memory allocation needed
+int numPrimes = std::count_if(multimap.keyBegin(), multimap.keyEnd(), isPrimeNumber);
+qDeleteAll(multimap2.keyBegin(), multimap2.keyEnd());
+//! [keyiterator2]