Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcontainertools_impl.h
Go to the documentation of this file.
1// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
2// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
3// Copyright (C) 2020 The Qt Company Ltd.
4// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
5// Qt-Security score:significant reason:default
6
7#if 0
8#pragma qt_sync_skip_header_check
9#pragma qt_sync_stop_processing
10#endif
11
12#ifndef QCONTAINERTOOLS_IMPL_H
13#define QCONTAINERTOOLS_IMPL_H
14
15#include <QtCore/qglobal.h>
16#include <QtCore/qtypeinfo.h>
17
18#include <QtCore/q20type_traits.h>
19#include <QtCore/qxptype_traits.h>
20
21#include <cstring>
22#include <iterator>
23#include <memory>
24#include <algorithm>
25
26QT_BEGIN_NAMESPACE
27
28namespace QtPrivate
29{
30
31/*!
32 \internal
33
34 Returns whether \a p is within a range [b, e). In simplest form equivalent to:
35 b <= p < e.
36*/
37template<typename T>
38static constexpr bool q_points_into_range(const T *p, const T *b, const T *e)
39{
40 if (q20::is_constant_evaluated()) {
41 for (; b != e; ++b) {
42 if (b == p)
43 return true;
44 }
45 return false;
46 }
47 return !std::less{}(p, b) && std::less{}(p, e);
48}
49
50/*!
51 \internal
52
53 Returns whether \a p is within container \a c. In its simplest form equivalent to:
54 c.data() <= p < c.data() + c.size()
55*/
56template <typename C, typename T>
57static constexpr bool q_points_into_range(const T &p, const C &c) noexcept
58{
59 static_assert(std::is_same_v<decltype(std::data(c)), T>);
60
61 // std::distance because QArrayDataPointer has a "qsizetype size"
62 // member but no size() function
63 return q_points_into_range(p, std::data(c),
64 std::data(c) + std::distance(std::begin(c), std::end(c)));
65}
66
67QT_WARNING_PUSH
68QT_WARNING_DISABLE_GCC("-Wmaybe-uninitialized")
69
70template <typename T, typename N>
78
79template <typename T, typename N>
80void q_uninitialized_relocate_n(T* first, N n, T* out)
81{
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.");
85 if (n != N(0)) { // even if N == 0, out == nullptr or first == nullptr are UB for memcpy()
86 std::memcpy(static_cast<void *>(out),
87 static_cast<const void *>(first),
88 n * sizeof(T));
89 }
90 } else {
91 q_uninitialized_move_if_noexcept_n(first, n, out);
92 if constexpr (QTypeInfo<T>::isComplex)
93 std::destroy_n(first, n);
94 }
95}
96
98
99/*!
100 \internal
101
102 A wrapper around std::rotate(), with an optimization for
103 Q_RELOCATABLE_TYPEs. We omit the return value, as it would be more work to
104 compute in the Q_RELOCATABLE_TYPE case and, unlike std::rotate on
105 ForwardIterators, callers can compute the result in constant time
106 themselves.
107*/
108template <typename T>
110{
111 if constexpr (QTypeInfo<T>::isRelocatable) {
112 const auto cast = [](T *p) { return reinterpret_cast<uchar*>(p); };
114 } else {
115 std::rotate(first, mid, last);
116 }
117}
118
119/*!
120 \internal
121 Copies all elements, except the ones for which \a pred returns \c true, from
122 range [first, last), to the uninitialized memory buffer starting at \a out.
123
124 It's undefined behavior if \a out points into [first, last).
125
126 Returns a pointer one past the last copied element.
127
128 If an exception is thrown, all the already copied elements in the destination
129 buffer are destroyed.
130*/
131template <typename T, typename Predicate>
132T *q_uninitialized_remove_copy_if(T *first, T *last, T *out, Predicate &pred)
133{
134 static_assert(std::is_nothrow_destructible_v<T>,
135 "This algorithm requires that T has a non-throwing destructor");
136 Q_ASSERT(!q_points_into_range(out, first, last));
137
138 T *dest_begin = out;
139 QT_TRY {
140 while (first != last) {
141 if (!pred(*first)) {
142 new (std::addressof(*out)) T(*first);
143 ++out;
144 }
145 ++first;
146 }
147 } QT_CATCH (...) {
148 std::destroy(std::reverse_iterator(out), std::reverse_iterator(dest_begin));
149 QT_RETHROW;
150 }
151 return out;
152}
153
154template<typename iterator, typename N>
155void q_relocate_overlap_n_left_move(iterator first, N n, iterator d_first)
156{
157 // requires: [first, n) is a valid range
158 // requires: d_first + n is reachable from d_first
159 // requires: iterator is at least a random access iterator
160 // requires: value_type(iterator) has a non-throwing destructor
161
162 Q_ASSERT(n);
163 Q_ASSERT(d_first < first); // only allow moves to the "left"
164 using T = typename std::iterator_traits<iterator>::value_type;
165
166 // Watches passed iterator. Unless commit() is called, all the elements that
167 // the watched iterator passes through are deleted at the end of object
168 // lifetime. freeze() could be used to stop watching the passed iterator and
169 // remain at current place.
170 //
171 // requires: the iterator is expected to always point to an invalid object
172 // (to uninitialized memory)
173 struct Destructor
174 {
175 iterator *iter;
176 iterator end;
177 iterator intermediate;
178
179 Destructor(iterator &it) noexcept : iter(std::addressof(it)), end(it) { }
180 void commit() noexcept { iter = std::addressof(end); }
181 void freeze() noexcept
182 {
183 intermediate = *iter;
184 iter = std::addressof(intermediate);
185 }
186 ~Destructor() noexcept
187 {
188 for (const int step = *iter < end ? 1 : -1; *iter != end;) {
189 std::advance(*iter, step);
190 (*iter)->~T();
191 }
192 }
193 } destroyer(d_first);
194
195 const iterator d_last = d_first + n;
196 // Note: use pair and explicitly copy iterators from it to prevent
197 // accidental reference semantics instead of copy. equivalent to:
198 //
199 // auto [overlapBegin, overlapEnd] = std::minmax(d_last, first);
200 auto pair = std::minmax(d_last, first);
201
202 // overlap area between [d_first, d_first + n) and [first, first + n) or an
203 // uninitialized memory area between the two ranges
204 iterator overlapBegin = pair.first;
205 iterator overlapEnd = pair.second;
206
207 // move construct elements in uninitialized region
208 while (d_first != overlapBegin) {
209 // account for std::reverse_iterator, cannot use new(d_first) directly
210 new (std::addressof(*d_first)) T(std::move_if_noexcept(*first));
211 ++d_first;
212 ++first;
213 }
214
215 // cannot commit but have to stop - there might be an overlap region
216 // which we don't want to delete (because it's part of existing data)
217 destroyer.freeze();
218
219 // move assign elements in overlap region
220 while (d_first != d_last) {
221 *d_first = std::move_if_noexcept(*first);
222 ++d_first;
223 ++first;
224 }
225
226 Q_ASSERT(d_first == destroyer.end + n);
227 destroyer.commit(); // can commit here as ~T() below does not throw
228
229 while (first != overlapEnd)
230 (--first)->~T();
231}
232
233/*!
234 \internal
235
236 Relocates a range [first, n) to [d_first, n) taking care of potential memory
237 overlaps. This is a generic equivalent of memmove.
238
239 If an exception is thrown during the relocation, all the relocated elements
240 are destroyed and [first, n) may contain valid but unspecified values,
241 including moved-from values (basic exception safety).
242*/
243template<typename T, typename N>
244void q_relocate_overlap_n(T *first, N n, T *d_first)
245{
246 static_assert(std::is_nothrow_destructible_v<T>,
247 "This algorithm requires that T has a non-throwing destructor");
248
249 if (n == N(0) || first == d_first || first == nullptr || d_first == nullptr)
250 return;
251
252 if constexpr (QTypeInfo<T>::isRelocatable) {
253 std::memmove(static_cast<void *>(d_first), static_cast<const void *>(first), n * sizeof(T));
254 } else { // generic version has to be used
255 if (d_first < first) {
256 q_relocate_overlap_n_left_move(first, n, d_first);
257 } else { // first < d_first
258 auto rfirst = std::make_reverse_iterator(first + n);
259 auto rd_first = std::make_reverse_iterator(d_first + n);
260 q_relocate_overlap_n_left_move(rfirst, n, rd_first);
261 }
262 }
263}
264
265template <typename T>
267{
268 T t;
269 T *operator->() noexcept { return &t; }
270};
271
272template <typename Iterator>
275 bool>::type;
276
277template <typename Iterator>
280 bool>::type;
281
282template <typename Iterator>
285 bool>::type;
286
287template <typename Container,
288 typename InputIterator,
289 IfIsNotForwardIterator<InputIterator> = true>
290void reserveIfForwardIterator(Container *, InputIterator, InputIterator)
291{
292}
293
294template <typename Container,
295 typename ForwardIterator,
296 IfIsForwardIterator<ForwardIterator> = true>
297void reserveIfForwardIterator(Container *c, ForwardIterator f, ForwardIterator l)
298{
299 c->reserve(static_cast<typename Container::size_type>(std::distance(f, l)));
300}
301
302template <typename Iterator>
303using KeyAndValueTest = decltype(
304 std::declval<Iterator &>().key(),
305 std::declval<Iterator &>().value()
306);
307
308template <typename Iterator>
309using FirstAndSecondTest = decltype(
310 (*std::declval<Iterator &>()).first,
311 (*std::declval<Iterator &>()).second
312);
313
314template <typename Iterator>
317
318template <typename Iterator>
324 >, bool>;
325
326template <typename Iterator>
327using MoveBackwardsTest = decltype(
328 std::declval<Iterator &>().operator--()
329);
330
331template <typename Iterator>
334
335template <typename T, typename U>
337 typename std::enable_if<!std::is_same<T, U>::value, bool>::type;
338
339template<typename T, typename U>
341
342template <typename Container, typename Predicate>
343auto sequential_erase_if(Container &c, Predicate &pred)
344{
345 // This is remove_if() modified to perform the find_if step on
346 // const_iterators to avoid shared container detaches if nothing needs to
347 // be removed. We cannot run remove_if after find_if: doing so would apply
348 // the predicate to the first matching element twice!
349
350 const auto cbegin = c.cbegin();
351 const auto cend = c.cend();
352 const auto t_it = std::find_if(cbegin, cend, pred);
353 auto result = std::distance(cbegin, t_it);
354 if (result == c.size())
355 return result - result; // `0` of the right type
356
357 // now detach:
358 const auto e = c.end();
359
360 auto it = std::next(c.begin(), result);
361 auto dest = it;
362
363 // Loop Invariants:
364 // - it != e
365 // - [next(it), e[ still to be checked
366 // - [c.begin(), dest[ are result
367 while (++it != e) {
368 if (!pred(*it)) {
369 *dest = std::move(*it);
370 ++dest;
371 }
372 }
373
374 result = std::distance(dest, e);
375 c.erase(dest, e);
376 return result;
377}
378
379template <typename Container, typename T>
380auto sequential_erase(Container &c, const T &t)
381{
382 // use the equivalence relation from http://eel.is/c++draft/list.erasure#1
383 auto cmp = [&](const auto &e) -> bool { return e == t; };
384 return sequential_erase_if(c, cmp); // can't pass rvalues!
385}
386
387template <typename Container, typename T>
388auto sequential_erase_with_copy(Container &c, const T &t)
389{
390 using CopyProxy = std::conditional_t<std::is_copy_constructible_v<T>, T, const T &>;
391 return sequential_erase(c, CopyProxy(t));
392}
393
394template <typename Container, typename T>
395auto sequential_erase_one(Container &c, const T &t)
396{
397 const auto cend = c.cend();
398 const auto it = std::find(c.cbegin(), cend, t);
399 if (it == cend)
400 return false;
401 c.erase(it);
402 return true;
403}
404
405template <typename T, typename Predicate>
406qsizetype qset_erase_if(QSet<T> &set, Predicate &pred)
407{
408 qsizetype result = 0;
409 auto it = set.cbegin();
410 auto e = set.cend(); // stable across detach (QHash::end() is a stateless sentinel)...
411 while (it != e) {
412 if (pred(*it)) {
413 ++result;
414 it = set.erase(it);
415 e = set.cend(); // ...but re-set nonetheless, in case at some point it won't be
416 } else {
417 ++it;
418 }
419 }
420 return result;
421}
422
423
424// Prerequisite: F is invocable on ArgTypes
425template <typename R, typename F, typename ... ArgTypes>
428
429// is_invocable_r checks for implicit conversions, but we need to check
430// for explicit conversions in remove_if. So, roll our own trait.
431template <typename R, typename F, typename ... ArgTypes>
432constexpr bool is_invocable_explicit_r_v = std::conjunction_v<
433 std::is_invocable<F, ArgTypes...>,
435>;
436
437template <typename Container, typename Predicate>
438auto associative_erase_if(Container &c, Predicate &pred)
439{
440 // we support predicates callable with either Container::iterator
441 // or with std::pair<const Key &, Value &>
442 using Iterator = typename Container::iterator;
443 using Key = typename Container::key_type;
444 using Value = typename Container::mapped_type;
445 using KeyValuePair = std::pair<const Key &, Value &>;
446
447 typename Container::size_type result = 0;
448
449 auto it = c.begin();
450 const auto e = c.end();
451 while (it != e) {
452 if constexpr (is_invocable_explicit_r_v<bool, Predicate &, Iterator &>) {
453 if (pred(it)) {
454 it = c.erase(it);
455 ++result;
456 } else {
457 ++it;
458 }
459 } else if constexpr (is_invocable_explicit_r_v<bool, Predicate &, KeyValuePair &&>) {
460 KeyValuePair p(it.key(), it.value());
461 if (pred(std::move(p))) {
462 it = c.erase(it);
463 ++result;
464 } else {
465 ++it;
466 }
467 } else {
468 static_assert(type_dependent_false<Container>(), "Predicate has an incompatible signature");
469 }
470 }
471
472 return result;
473}
474
475} // namespace QtPrivate
476
477QT_END_NAMESPACE
478
479#endif // QCONTAINERTOOLS_IMPL_H
\inmodule QtCore
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
#define __has_include(x)
static bool nameMatch(const char *a, QAnyStringView b)
static const uchar utf8bom[]
static QChar * fromUtf32LE(QChar *out, QByteArrayView in, QStringConverter::State *state)
@ HeaderDone
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)