diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2016-03-03 20:20:42 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2019-04-17 08:11:21 +0000 |
commit | 2e1763d83a1dacfc5b747934fb77fa7cec7bfe47 (patch) | |
tree | 6b5eb92a868b7227c5391d6cf82f0fb930e3adb9 /src/corelib/tools/qset.h | |
parent | f363540580eabd5aa27f4c172da796d637e38dfa (diff) |
Non-associative containers: add range constructors
Something nice we'd like to detect for array-backed containers
is if the iterator passed is a Contiguous one; if the type is also
trivially copyable / Q_PRIMITIVE_TYPE, we could memcpy() the whole
range.
However, there's no trait in the Standard to detect contiguous
iterators (the best approximation would be detecting if the iterator
is actually a pointer). Also, it's probably not smart to do the work
now for QVector since QVector needs refactoring anyhow, and this work
will be lost.
QString and QByteArray are left in another commit.
[ChangeLog][QtCore][QVector] Added range constructor.
[ChangeLog][QtCore][QVarLengthArray] Added range constructor.
[ChangeLog][QtCore][QList] Added range constructor.
[ChangeLog][QtCore][QStringList] Added range constructor.
[ChangeLog][QtCore][QLinkedList] Added range constructor.
[ChangeLog][QtCore][QSet] Added range constructor.
Change-Id: I220edb796053c9c4d31a6dbdc7efc5fc0f6678f9
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Diffstat (limited to 'src/corelib/tools/qset.h')
-rw-r--r-- | src/corelib/tools/qset.h | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h index aa915f7ed1..7ae715d247 100644 --- a/src/corelib/tools/qset.h +++ b/src/corelib/tools/qset.h @@ -41,6 +41,8 @@ #define QSET_H #include <QtCore/qhash.h> +#include <QtCore/qcontainertools_impl.h> + #ifdef Q_COMPILER_INITIALIZER_LISTS #include <initializer_list> #endif @@ -59,12 +61,16 @@ public: inline QSet() noexcept {} #ifdef Q_COMPILER_INITIALIZER_LISTS inline QSet(std::initializer_list<T> list) + : QSet(list.begin(), list.end()) {} +#endif + template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> + inline QSet(InputIterator first, InputIterator last) { - reserve(int(list.size())); - for (typename std::initializer_list<T>::const_iterator it = list.begin(); it != list.end(); ++it) - insert(*it); + QtPrivate::reserveIfForwardIterator(this, first, last); + for (; first != last; ++first) + insert(*first); } -#endif + // compiler-generated copy/move ctor/assignment operators are fine! // compiler-generated destructor is fine! |