summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-07 16:54:20 +0200
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2020-08-10 16:35:59 +0200
commit7406a8b45a6fc196ed55b71f5ae953c54199dbb6 (patch)
treec8d19c57ab9266f09cafe51d2c49b8f342b77590
parent046ad5a4426ea1be7f7c04c0681ae62ef16c8599 (diff)
QMultiMap: add constructors from QMap
We can provide those. They don't lose information, do not have the problem we face at offering ranged constructors (namely the order of duplicate keys), and have a distinct advantage over ranged constructors: a non-shared rvalue QMap can be "upgraded" to a QMultiMap without allocating memory for the multimap. Change-Id: Ic23c83927c05a210bc1f0050006c9f26365d3916 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/tools/qmap.h31
-rw-r--r--src/corelib/tools/qmultimap.qdoc13
2 files changed, 44 insertions, 0 deletions
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index afc4ca37c4..65b3ba55db 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -220,6 +220,8 @@ class QMap
using MapData = QMapData<std::map<Key, T>>;
QtPrivate::QExplicitlySharedDataPointerV2<MapData> d;
+ friend class QMultiMap<Key, T>;
+
public:
using key_type = Key;
using mapped_type = T;
@@ -778,6 +780,35 @@ public:
qSwap(d, other.d);
}
+ explicit QMultiMap(const QMap<Key, T> &other)
+ : d(other.isEmpty() ? nullptr : new MapData)
+ {
+ if (d) {
+ Q_ASSERT(other.d);
+ d->m.insert(other.d->m.begin(),
+ other.d->m.end());
+ }
+ }
+
+ explicit QMultiMap(QMap<Key, T> &&other)
+ : d(other.isEmpty() ? nullptr : new MapData)
+ {
+ if (d) {
+ Q_ASSERT(other.d);
+ if (other.d.isShared()) {
+ d->m.insert(other.d->m.begin(),
+ other.d->m.end());
+ } else {
+#ifdef __cpp_lib_node_extract
+ d->m.merge(std::move(other.d->m));
+#else
+ d->m.insert(std::make_move_iterator(other.d->m.begin()),
+ std::make_move_iterator(other.d->m.end()));
+#endif
+ }
+ }
+ }
+
explicit QMultiMap(const std::multimap<Key, T> &other)
: d(other.empty() ? nullptr : new MapData(other))
{
diff --git a/src/corelib/tools/qmultimap.qdoc b/src/corelib/tools/qmultimap.qdoc
index 0de4a6c8fd..9db495db1e 100644
--- a/src/corelib/tools/qmultimap.qdoc
+++ b/src/corelib/tools/qmultimap.qdoc
@@ -214,6 +214,19 @@
initializer list \a list.
*/
+/*! \fn template <class Key, class T> QMultiMap<Key, T>::QMultiMap(const QMap<Key, T> &other)
+ \since 6.0
+
+ Constructs a multi map as a copy of \a other.
+*/
+
+/*! \fn template <class Key, class T> QMultiMap<Key, T>::QMultiMap(QMap<Key, T> &&other)
+ \since 6.0
+
+ If \a other is shared, constructs a multi map as a copy of \a other.
+ Otherwise, constructs a multi map by moving the elements from \a other.
+*/
+
/*! \fn template <class Key, class T> QMultiMap<Key, T>::QMultiMap(const std::multimap<Key, T> &other)
Constructs a copy of \a other.