Short live q20::fill{,_n}!

It just adds constexpr to it (we're ignoring the range version).

Apply it to QStaticByteArrayMatcher, where it replaces rather
lengthy initialization code.

Pick-to: 6.4
Change-Id: I1d60216fb04c94fa66fce5cc01313b3e9ba856ac
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 e1d21fe813
commit fd2685c2f0
2 changed files with 28 additions and 25 deletions

View File

@ -32,6 +32,8 @@ namespace q20 {
using std::copy;
using std::copy_if;
using std::copy_n;
using std::fill;
using std::fill_n;
using std::is_sorted_until;
using std::is_sorted;
using std::transform;
@ -75,6 +77,28 @@ copy_n(InputIterator first, Size n, OutputIterator dest)
return dest;
}
template <typename ForwardIterator, typename Value>
constexpr void
fill(ForwardIterator first, ForwardIterator last, const Value &value)
{
while (first != last) {
*first = value;
++first;
}
}
template <typename OutputIterator, typename Size, typename Value>
constexpr OutputIterator
fill_n(OutputIterator first, Size n, const Value &value)
{
while (n > Size{0}) {
*first = value;
++first;
--n;
}
return first;
}
template <typename ForwardIterator, typename BinaryPredicate = std::less<>>
constexpr ForwardIterator
is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {})

View File

@ -6,6 +6,8 @@
#include <QtCore/qbytearray.h>
#include <QtCore/q20algorithm.h>
#include <iterator>
#include <limits>
QT_BEGIN_NAMESPACE
@ -83,31 +85,8 @@ private:
{
const auto uchar_max = (std::numeric_limits<uchar>::max)();
uchar max = n > uchar_max ? uchar_max : uchar(n);
Skiptable table = {
// this verbose initialization code aims to avoid some opaque error messages
// even on powerful compilers such as GCC 5.3. Even though for GCC a loop
// format can be found that v5.3 groks, it's probably better to go with this
// for the time being:
{
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
max, max, max, max, max, max, max, max, max, max, max, max, max, max, max, max,
}
};
Skiptable table = {};
q20::fill(std::begin(table.data), std::end(table.data), max);
pattern += n - max;
while (max--)
table.data[uchar(*pattern++)] = max;