Short live q20::transform()!

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 <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2022-10-07 16:50:42 +02:00
parent d6250e2a0d
commit d4e62a9768
2 changed files with 21 additions and 12 deletions

View File

@ -27,13 +27,14 @@
QT_BEGIN_NAMESPACE
namespace q20 {
// like std::is_sorted{,_until}, std::copy (ie. constexpr)
// like std::<algorithm> (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 <typename InputIterator, typename OutputIterator>
constexpr OutputIterator
@ -88,11 +89,27 @@ is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p =
}
return first;
}
template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
constexpr bool is_sorted(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})
{
return q20::is_sorted_until(first, last, p) == last;
}
template <typename InputIterator, typename OutputIterator, typename UnaryFunction>
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
}

View File

@ -70,16 +70,6 @@ private:
};
namespace QtPrivate {
// std::copy is not constexpr in C++17
template <typename II, typename OO>
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 <size_t Highest> constexpr auto minifyValue()
{
if constexpr (Highest <= (std::numeric_limits<quint8>::max)()) {
@ -137,7 +127,9 @@ constexpr auto makeOffsetStringArray(StringExtractor extractString, const T &...
// prepend zero
std::array<MinifiedOffsetType, Count + 1> 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<StringLength>(extractString, entries...);
return QOffsetStringArray(staticString, minifiedOffsetList);