From 7406a8b45a6fc196ed55b71f5ae953c54199dbb6 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Fri, 7 Aug 2020 16:54:20 +0200 Subject: 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 --- src/corelib/tools/qmap.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/corelib/tools/qmap.h') 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>; QtPrivate::QExplicitlySharedDataPointerV2 d; + friend class QMultiMap; + public: using key_type = Key; using mapped_type = T; @@ -778,6 +780,35 @@ public: qSwap(d, other.d); } + explicit QMultiMap(const QMap &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 &&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 &other) : d(other.empty() ? nullptr : new MapData(other)) { -- cgit v1.2.3