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/qnamespace.h>
17#include <QtCore/qtypeinfo.h>
19#include <QtCore/q20type_traits.h>
20#include <QtCore/qxptype_traits.h>
33
34
35
36
37
41 if (q20::is_constant_evaluated()) {
48 return !std::less{}(p, b) && std::less{}(p, e);
52
53
54
55
56
57template <
typename C,
typename T>
60 static_assert(std::is_same_v<
decltype(std::data(c)), T>);
64 return q_points_into_range(p,
std::data(c),
65 std::data(c) +
std::distance(
std::begin(c),
std::end(c)));
69QT_WARNING_DISABLE_GCC(
"-Wmaybe-uninitialized")
71template <
typename T,
typename N>
80template <
typename T,
typename N>
83 if constexpr (
QTypeInfo<T>::isRelocatable) {
84 static_assert(std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>,
85 "Refusing to relocate this non-copy/non-move-constructible type.");
91 std::memcpy(
static_cast<
void *>(out),
92 static_cast<
const void *>(first),
98 q_uninitialized_move_if_noexcept_n(first, n, out);
100 std::destroy_n(first, n);
107
108
109
110
111
112
113
114
119 const auto cast = [](
T *
p) {
return reinterpret_cast<
uchar*>(
p); };
127
128
129
130
131
132
133
134
135
136
137
138template <
typename T,
typename Predicate>
141 static_assert(std::is_nothrow_destructible_v<T>,
142 "This algorithm requires that T has a non-throwing destructor");
143 Q_ASSERT(!q_points_into_range(out, first, last));
147 while (first != last) {
149 new (std::addressof(*out)) T(*first);
155 std::destroy(std::reverse_iterator(out), std::reverse_iterator(dest_begin));
161template<
typename iterator,
typename N>
170 Q_ASSERT(d_first < first);
171 using T =
typename std::iterator_traits<iterator>::value_type;
184 iterator intermediate;
186 Destructor(iterator &it)
noexcept : iter(
std::addressof(it)), end(it) { }
187 void commit()
noexcept { iter =
std::addressof(end); }
188 void freeze()
noexcept
190 intermediate = *iter;
191 iter =
std::addressof(intermediate);
193 ~Destructor()
noexcept
195 for (
const int step = *iter < end ? 1 : -1; *iter != end;) {
196 std::advance(*iter, step);
200 } destroyer(d_first);
202 const iterator d_last = d_first + n;
207 auto pair =
std::minmax(d_last, first);
211 iterator overlapBegin = pair.first;
212 iterator overlapEnd = pair.second;
215 while (d_first != overlapBegin) {
217 new (
std::addressof(*d_first)) T(
std::move_if_noexcept(*first));
227 while (d_first != d_last) {
228 *d_first =
std::move_if_noexcept(*first);
233 Q_ASSERT(d_first == destroyer.end + n);
236 while (first != overlapEnd)
241
242
243
244
245
246
247
248
249
250template<
typename T,
typename N>
253 static_assert(std::is_nothrow_destructible_v<T>,
254 "This algorithm requires that T has a non-throwing destructor");
256 if (n == N(0) || first == d_first || first ==
nullptr || d_first ==
nullptr)
259 if constexpr (
QTypeInfo<T>::isRelocatable) {
260 std::memmove(
static_cast<
void *>(d_first),
static_cast<
const void *>(first), n *
sizeof(T));
262 if (d_first < first) {
263 q_relocate_overlap_n_left_move(first, n, d_first);
265 auto rfirst =
std::make_reverse_iterator(first + n);
266 auto rd_first =
std::make_reverse_iterator(d_first + n);
267 q_relocate_overlap_n_left_move(rfirst, n, rd_first);
294template <
typename Container,
295 typename InputIterator,
296 IfIsNotForwardIterator<InputIterator> =
true>
301template <
typename Container,
302 typename ForwardIterator,
303 IfIsForwardIterator<ForwardIterator> =
true>
306 c->reserve(
static_cast<
typename Container::size_type>(
std::distance(f, l)));
342template <
typename T,
typename U>
346template<
typename T,
typename U>
349template <
typename Container,
typename Predicate>
357 const auto cbegin = c.cbegin();
358 const auto cend = c.cend();
359 const auto t_it =
std::find_if(cbegin, cend, pred);
360 auto result =
std::distance(cbegin, t_it);
361 if (result == c.size())
362 return result - result;
365 const auto e = c.end();
367 auto it =
std::next(c.begin(), result);
376 *dest =
std::move(*it);
381 result =
std::distance(dest, e);
386template <
typename Container,
typename T>
390 auto cmp = [&](
const auto &e) ->
bool {
return e == t; };
391 return sequential_erase_if(c, cmp);
394template <
typename Container,
typename T>
397 using CopyProxy = std::conditional_t<std::is_copy_constructible_v<T>, T,
const T &>;
398 return sequential_erase(c, CopyProxy(t));
401template <
typename Container,
typename T>
404 const auto cend = c.cend();
405 const auto it =
std::find(c.cbegin(), cend, t);
412template <
typename String,
typename Predicate>
415 auto &d = str.data_ptr();
416 qsizetype result = 0;
420 if (!d.needsDetach()) {
422 auto it =
std::remove_if(d.begin(), end, pred);
424 result =
std::distance(it, end);
425 d->erase(it, result);
426 d.data()[d.size] =
'\0';
429 const auto begin = d.begin();
430 const auto end = d.end();
431 auto it =
std::find_if(begin, end, pred);
435 String copy{d.size, Qt::Uninitialized};
436 auto &other_d = copy.data_ptr();
437 Q_CHECK_PTR(other_d.data());
438 auto dest = other_d.begin();
440 dest =
std::uninitialized_copy(begin, it, dest);
441 dest = q_uninitialized_remove_copy_if(
std::next(it), end, dest, pred);
442 other_d.size =
std::distance(other_d.data(), dest);
443 other_d.data()[other_d.size] =
'\0';
444 result = d.size - other_d.size;
451template <
typename T,
typename Predicate>
454 qsizetype result = 0;
455 auto it = set.cbegin();
471template <
typename R,
typename F,
typename ... ArgTypes>
477template <
typename R,
typename F,
typename ...
ArgTypes>
479 std::is_invocable<F, ArgTypes...>,
483template <
typename Container,
typename Predicate>
488 using Iterator =
typename Container::iterator;
489 using Key =
typename Container::key_type;
490 using Value =
typename Container::mapped_type;
491 using KeyValuePair =
std::pair<
const Key &, Value &>;
493 typename Container::size_type result = 0;
496 const auto e = c.end();
498 if constexpr (is_invocable_explicit_r_v<
bool, Predicate &, Iterator &>) {
505 }
else if constexpr (is_invocable_explicit_r_v<
bool, Predicate &, KeyValuePair &&>) {
506 KeyValuePair p(it.key(), it.value());
507 if (pred(
std::move(p))) {
514 static_assert(type_dependent_false<Container>(),
"Predicate has an incompatible signature");
auto associative_erase_if(Container &c, Predicate &pred)
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)
constexpr bool q_points_into_range(const T &p, const C &c) noexcept
auto sequential_erase(Container &c, const T &t)
T * q_uninitialized_remove_copy_if(T *first, T *last, T *out, Predicate &pred)
qsizetype q_string_like_container_erase_if(String &str, Predicate pred)
constexpr bool q_points_into_range(const T *p, const T *b, const T *e)
void q_relocate_overlap_n(T *first, N n, T *d_first)
void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
constexpr bool is_invocable_explicit_r_v
QByteArray qParseHtmlMetaForEncoding(QByteArrayView data)
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 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 int partiallyParsedDataCount(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