From d4e62a9768c4ce4964f65cca15b3da0a36b910f1 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Fri, 7 Oct 2022 16:50:42 +0200 Subject: [PATCH] Short live q20::transform()! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It just adds constexpr to it (we're ignoring the range version). Apply it to QOffsetStringArray, where it replaces the copyData() function. Pick-to: 6.4 Change-Id: I6caf3b5fd2e60f4fcb0b116684c3ad6a8043f38e Reviewed-by: MÃ¥rten Nordheim --- src/corelib/global/q20algorithm.h | 19 ++++++++++++++++++- src/corelib/tools/qoffsetstringarray_p.h | 14 +++----------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/corelib/global/q20algorithm.h b/src/corelib/global/q20algorithm.h index bdfb3268130..1d2e0117f4c 100644 --- a/src/corelib/global/q20algorithm.h +++ b/src/corelib/global/q20algorithm.h @@ -27,13 +27,14 @@ QT_BEGIN_NAMESPACE namespace q20 { -// like std::is_sorted{,_until}, std::copy (ie. constexpr) +// like std:: (ie. not ranged, but constexpr) #ifdef __cpp_lib_constexpr_algorithms using std::copy; using std::copy_if; using std::copy_n; using std::is_sorted_until; using std::is_sorted; +using std::transform; #else template constexpr OutputIterator @@ -88,11 +89,27 @@ is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = } return first; } + template > constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {}) { return q20::is_sorted_until(first, last, p) == last; } + +template +constexpr OutputIterator +transform(InputIterator first, InputIterator last, OutputIterator dest, UnaryFunction op) +{ + while (first != last) { + *dest = op(*first); + ++first; + ++dest; + } + return dest; +} + +// binary transform missing on purpose (no users) + #endif } diff --git a/src/corelib/tools/qoffsetstringarray_p.h b/src/corelib/tools/qoffsetstringarray_p.h index 40834871751..2a0e6de55b0 100644 --- a/src/corelib/tools/qoffsetstringarray_p.h +++ b/src/corelib/tools/qoffsetstringarray_p.h @@ -70,16 +70,6 @@ private: }; namespace QtPrivate { -// std::copy is not constexpr in C++17 -template -static constexpr OO copyData(II input, qsizetype n, OO output) -{ - using E = decltype(+*output); - for (qsizetype i = 0; i < n; ++i) - output[i] = E(input[i]); - return output + n; -} - template constexpr auto minifyValue() { if constexpr (Highest <= (std::numeric_limits::max)()) { @@ -137,7 +127,9 @@ constexpr auto makeOffsetStringArray(StringExtractor extractString, const T &... // prepend zero std::array minifiedOffsetList = {}; - QtPrivate::copyData(fullOffsetList.begin(), Count, minifiedOffsetList.begin() + 1); + q20::transform(fullOffsetList.begin(), fullOffsetList.end(), + minifiedOffsetList.begin() + 1, + [] (auto e) { return MinifiedOffsetType(e); }); std::array staticString = QtPrivate::makeStaticString(extractString, entries...); return QOffsetStringArray(staticString, minifiedOffsetList);