8#pragma qt_sync_skip_header_check
9#pragma qt_sync_stop_processing
12#ifndef QCONTAINERTOOLS_IMPL_H
13#define QCONTAINERTOOLS_IMPL_H
15#include <QtCore/qglobal.h>
16#include <QtCore/qtypeinfo.h>
18#include <QtCore/q20type_traits.h>
19#include <QtCore/qxptype_traits.h>
32
33
34
35
36
40 if (q20::is_constant_evaluated()) {
47 return !std::less{}(p, b) && std::less{}(p, e);
51
52
53
54
55
56template <
typename C,
typename T>
59 static_assert(std::is_same_v<
decltype(std::data(c)), T>);
63 return q_points_into_range(p,
std::data(c),
64 std::data(c) +
std::distance(
std::begin(c),
std::end(c)));
68QT_WARNING_DISABLE_GCC(
"-Wmaybe-uninitialized")
70template <
typename T,
typename N>
79template <
typename T,
typename N>
82 if constexpr (
QTypeInfo<T>::isRelocatable) {
83 static_assert(std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>,
84 "Refusing to relocate this non-copy/non-move-constructible type.");
90 std::memcpy(
static_cast<
void *>(out),
91 static_cast<
const void *>(first),
97 q_uninitialized_move_if_noexcept_n(first, n, out);
99 std::destroy_n(first, n);
106
107
108
109
110
111
112
113
118 const auto cast = [](
T *
p) {
return reinterpret_cast<
uchar*>(
p); };
126
127
128
129
130
131
132
133
134
135
136
137template <
typename T,
typename Predicate>
140 static_assert(std::is_nothrow_destructible_v<T>,
141 "This algorithm requires that T has a non-throwing destructor");
142 Q_ASSERT(!q_points_into_range(out, first, last));
146 while (first != last) {
148 new (std::addressof(*out)) T(*first);
154 std::destroy(std::reverse_iterator(out), std::reverse_iterator(dest_begin));
160template<
typename iterator,
typename N>
169 Q_ASSERT(d_first < first);
170 using T =
typename std::iterator_traits<iterator>::value_type;
183 iterator intermediate;
185 Destructor(iterator &it)
noexcept : iter(
std::addressof(it)), end(it) { }
186 void commit()
noexcept { iter =
std::addressof(end); }
187 void freeze()
noexcept
189 intermediate = *iter;
190 iter =
std::addressof(intermediate);
192 ~Destructor()
noexcept
194 for (
const int step = *iter < end ? 1 : -1; *iter != end;) {
195 std::advance(*iter, step);
199 } destroyer(d_first);
201 const iterator d_last = d_first + n;
206 auto pair =
std::minmax(d_last, first);
210 iterator overlapBegin = pair.first;
211 iterator overlapEnd = pair.second;
214 while (d_first != overlapBegin) {
216 new (
std::addressof(*d_first)) T(
std::move_if_noexcept(*first));
226 while (d_first != d_last) {
227 *d_first =
std::move_if_noexcept(*first);
232 Q_ASSERT(d_first == destroyer.end + n);
235 while (first != overlapEnd)
240
241
242
243
244
245
246
247
248
249template<
typename T,
typename N>
252 static_assert(std::is_nothrow_destructible_v<T>,
253 "This algorithm requires that T has a non-throwing destructor");
255 if (n == N(0) || first == d_first || first ==
nullptr || d_first ==
nullptr)
258 if constexpr (
QTypeInfo<T>::isRelocatable) {
259 std::memmove(
static_cast<
void *>(d_first),
static_cast<
const void *>(first), n *
sizeof(T));
261 if (d_first < first) {
262 q_relocate_overlap_n_left_move(first, n, d_first);
264 auto rfirst =
std::make_reverse_iterator(first + n);
265 auto rd_first =
std::make_reverse_iterator(d_first + n);
266 q_relocate_overlap_n_left_move(rfirst, n, rd_first);
293template <
typename Container,
294 typename InputIterator,
295 IfIsNotForwardIterator<InputIterator> =
true>
300template <
typename Container,
301 typename ForwardIterator,
302 IfIsForwardIterator<ForwardIterator> =
true>
305 c->reserve(
static_cast<
typename Container::size_type>(
std::distance(f, l)));
341template <
typename T,
typename U>
345template<
typename T,
typename U>
348template <
typename Container,
typename Predicate>
356 const auto cbegin = c.cbegin();
357 const auto cend = c.cend();
358 const auto t_it =
std::find_if(cbegin, cend, pred);
359 auto result =
std::distance(cbegin, t_it);
360 if (result == c.size())
361 return result - result;
364 const auto e = c.end();
366 auto it =
std::next(c.begin(), result);
375 *dest =
std::move(*it);
380 result =
std::distance(dest, e);
385template <
typename Container,
typename T>
389 auto cmp = [&](
const auto &e) ->
bool {
return e == t; };
390 return sequential_erase_if(c, cmp);
393template <
typename Container,
typename T>
396 using CopyProxy = std::conditional_t<std::is_copy_constructible_v<T>, T,
const T &>;
397 return sequential_erase(c, CopyProxy(t));
400template <
typename Container,
typename T>
403 const auto cend = c.cend();
404 const auto it =
std::find(c.cbegin(), cend, t);
411template <
typename T,
typename Predicate>
414 qsizetype result = 0;
415 auto it = set.cbegin();
431template <
typename R,
typename F,
typename ... ArgTypes>
437template <
typename R,
typename F,
typename ...
ArgTypes>
439 std::is_invocable<F, ArgTypes...>,
443template <
typename Container,
typename Predicate>
448 using Iterator =
typename Container::iterator;
449 using Key =
typename Container::key_type;
450 using Value =
typename Container::mapped_type;
451 using KeyValuePair =
std::pair<
const Key &, Value &>;
453 typename Container::size_type result = 0;
456 const auto e = c.end();
458 if constexpr (is_invocable_explicit_r_v<
bool, Predicate &, Iterator &>) {
465 }
else if constexpr (is_invocable_explicit_r_v<
bool, Predicate &, KeyValuePair &&>) {
466 KeyValuePair p(it.key(), it.value());
467 if (pred(
std::move(p))) {
474 static_assert(type_dependent_false<Container>(),
"Predicate has an incompatible signature");
auto associative_erase_if(Container &c, Predicate &pred)
static int partiallyParsedDataCount(QStringConverter::State *state)
void q_uninitialized_relocate_n(T *first, N n, T *out)
qsizetype qset_erase_if(QSet< T > &set, Predicate &pred)
auto sequential_erase_one(Container &c, const T &t)
void q_relocate_overlap_n_left_move(iterator first, N n, iterator d_first)
auto sequential_erase_if(Container &c, Predicate &pred)
auto sequential_erase_with_copy(Container &c, const T &t)
auto sequential_erase(Container &c, const T &t)
T * q_uninitialized_remove_copy_if(T *first, T *last, T *out, Predicate &pred)
static constexpr bool q_points_into_range(const T &p, const C &c) noexcept
void q_relocate_overlap_n(T *first, N n, T *d_first)
static constexpr bool q_points_into_range(const T *p, const T *b, const T *e)
void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
constexpr bool is_invocable_explicit_r_v
static bool nameMatch(const char *a, QAnyStringView b)
static const uchar utf8bom[]
static QChar * fromUtf32LE(QChar *out, QByteArrayView in, QStringConverter::State *state)
static QChar * fromUtf16LE(QChar *out, QByteArrayView in, QStringConverter::State *state)
static QByteArray parseHtmlMetaForEncoding(QByteArrayView data)
static QChar * fromUtf32BE(QChar *out, QByteArrayView in, QStringConverter::State *state)
static qsizetype toUtf8Len(qsizetype l)
static QChar * fromLocal8Bit(QChar *out, QByteArrayView in, QStringConverter::State *state)
static QChar * fromUtf16(QChar *out, QByteArrayView in, QStringConverter::State *state)
static qsizetype toLatin1Len(qsizetype l)
static bool nameMatch_impl_impl(const char *a, const Char *b, const Char *b_end)
static bool nameMatch_impl(const char *a, QLatin1StringView b)
static QChar * fromUtf32(QChar *out, QByteArrayView in, QStringConverter::State *state)
static char * toUtf32(char *out, QStringView in, QStringConverter::State *state)
static char * toUtf16LE(char *out, QStringView in, QStringConverter::State *state)
static qsizetype fromUtf8Len(qsizetype l)
static char * toLocal8Bit(char *out, QStringView in, QStringConverter::State *state)
static qsizetype toUtf16Len(qsizetype l)
static qsizetype fromLatin1Len(qsizetype l)
static char * toUtf16BE(char *out, QStringView in, QStringConverter::State *state)
static char * toUtf32LE(char *out, QStringView in, QStringConverter::State *state)
static qsizetype fromUtf32Len(qsizetype l)
static qsizetype availableCodecCount()
static QChar * fromUtf16BE(QChar *out, QByteArrayView in, QStringConverter::State *state)
static qsizetype toUtf32Len(qsizetype l)
static char * toUtf16(char *out, QStringView in, QStringConverter::State *state)
static qsizetype fromUtf16Len(qsizetype l)
static char * toUtf32BE(char *out, QStringView in, QStringConverter::State *state)
static void appendUtf16(const NoOutput &, char16_t)
static void appendUcs4(const NoOutput &, char32_t)
T * operator->() noexcept