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
qarraydataops.h
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// Copyright (C) 2016 Intel Corporation.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4// Qt-Security score:significant reason:default
5
6#ifndef QARRAYDATAOPS_H
7#define QARRAYDATAOPS_H
8
9#include <QtCore/qarraydata.h>
10#include <QtCore/qcontainertools_impl.h>
11#include <QtCore/qnamespace.h>
12
13#include <QtCore/q20functional.h>
14#include <QtCore/q20memory.h>
15#include <new>
16#include <string.h>
17#include <utility>
18#include <iterator>
19#include <type_traits>
20
21QT_BEGIN_NAMESPACE
22
23template <class T> struct QArrayDataPointer;
24
25namespace QtPrivate {
26
27template <class T>
29{
30 static_assert (std::is_nothrow_destructible_v<T>, "Types with throwing destructors are not supported in Qt containers.");
31
32protected:
35
36private:
37 DataPointer *m_ptr;
38
39public:
40 explicit QPodArrayOps(DataPointer &dp) : m_ptr{&dp} {}
41
43 { return m_ptr; }
44 const DataPointer *that() const
45 { return m_ptr; }
46
48
49 void copyAppend(const T *b, const T *e) noexcept
50 {
51 Q_ASSERT(that()->isMutable() || b == e);
52 Q_ASSERT(!that()->isShared() || b == e);
53 Q_ASSERT(b <= e);
54 Q_ASSERT((e - b) <= that()->freeSpaceAtEnd());
55
56 if (b == e)
57 return;
58
59 ::memcpy(static_cast<void *>(that()->end()), static_cast<const void *>(b), (e - b) * sizeof(T));
60 that()->size += (e - b);
61 }
62
63 void copyAppend(qsizetype n, parameter_type t) noexcept
64 {
65 Q_ASSERT(!that()->isShared() || n == 0);
66 Q_ASSERT(that()->freeSpaceAtEnd() >= n);
67 if (!n)
68 return;
69
70 T *where = that()->end();
71 that()->size += qsizetype(n);
72 while (n--)
73 *where++ = t;
74 }
75
76 void moveAppend(T *b, T *e) noexcept
77 {
79 }
80
81 void truncate(size_t newSize) noexcept
82 {
83 Q_ASSERT(that()->isMutable());
84 Q_ASSERT(!that()->isShared());
85 Q_ASSERT(newSize <= size_t(that()->size));
86
87 that()->size = qsizetype(newSize);
88 }
89
90 void destroyAll() noexcept // Call from destructors, ONLY!
91 {
92 Q_ASSERT(that()->d);
93 Q_ASSERT(that()->d->ref_.loadRelaxed() == 0);
94
95 // As this is to be called only from destructor, it doesn't need to be
96 // exception safe; size not updated.
97 }
98
99 T *createHole(QArrayData::GrowthPosition pos, qsizetype where, qsizetype n)
100 {
101 Q_ASSERT((pos == QArrayData::GrowsAtBeginning && n <= that()->freeSpaceAtBegin()) ||
102 (pos == QArrayData::GrowsAtEnd && n <= that()->freeSpaceAtEnd()));
103
104 T *insertionPoint = that()->ptr + where;
105 if (pos == QArrayData::GrowsAtEnd) {
106 if (where < that()->size)
107 ::memmove(static_cast<void *>(insertionPoint + n), static_cast<void *>(insertionPoint), (that()->size - where) * sizeof(T));
108 } else {
109 Q_ASSERT(where == 0);
110 that()->ptr -= n;
111 insertionPoint -= n;
112 }
113 that()->size += n;
114 return insertionPoint;
115 }
116
117 void insert(qsizetype i, const T *data, qsizetype n)
118 {
119 typename Data::GrowthPosition pos = Data::GrowsAtEnd;
120 if (that()->size != 0 && i == 0)
121 pos = Data::GrowsAtBeginning;
122
123 DataPointer oldData;
124 that()->detachAndGrow(pos, n, &data, &oldData);
125 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
126 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
127
128 T *where = createHole(pos, i, n);
129 ::memcpy(static_cast<void *>(where), static_cast<const void *>(data), n * sizeof(T));
130 }
131
132 void insert(qsizetype i, qsizetype n, parameter_type t)
133 {
134 T copy(t);
135
136 typename Data::GrowthPosition pos = Data::GrowsAtEnd;
137 if (that()->size != 0 && i == 0)
138 pos = Data::GrowsAtBeginning;
139
140 that()->detachAndGrow(pos, n, nullptr, nullptr);
141 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
142 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
143
144 T *where = createHole(pos, i, n);
145 while (n--)
146 *where++ = copy;
147 }
148
149 template<typename... Args>
150 void emplace(qsizetype i, Args &&... args)
151 {
152 bool detach = that()->needsDetach();
153 if (!detach) {
154 if (i == that()->size && that()->freeSpaceAtEnd()) {
155 new (that()->end()) T(std::forward<Args>(args)...);
156 ++that()->size;
157 return;
158 }
159 if (i == 0 && that()->freeSpaceAtBegin()) {
160 new (that()->begin() - 1) T(std::forward<Args>(args)...);
161 --that()->ptr;
162 ++that()->size;
163 return;
164 }
165 }
166 T tmp(std::forward<Args>(args)...);
167 typename QArrayData::GrowthPosition pos = QArrayData::GrowsAtEnd;
168 if (that()->size != 0 && i == 0)
169 pos = QArrayData::GrowsAtBeginning;
170
171 that()->detachAndGrow(pos, 1, nullptr, nullptr);
172
173 T *where = createHole(pos, i, 1);
174 new (where) T(std::move(tmp));
175 }
176
177 void erase(T *b, qsizetype n)
178 {
179 T *e = b + n;
180 Q_ASSERT(that()->isMutable());
181 Q_ASSERT(b < e);
182 Q_ASSERT(b >= that()->begin() && b < that()->end());
183 Q_ASSERT(e > that()->begin() && e <= that()->end());
184
185 // Comply with std::vector::erase(): erased elements and all after them
186 // are invalidated. However, erasing from the beginning effectively
187 // means that all iterators are invalidated. We can use this freedom to
188 // erase by moving towards the end.
189 if (b == that()->begin() && e != that()->end()) {
190 that()->ptr = e;
191 } else if (e != that()->end()) {
192 ::memmove(static_cast<void *>(b), static_cast<void *>(e),
193 (static_cast<T *>(that()->end()) - e) * sizeof(T));
194 }
195 that()->size -= n;
196 }
197
198 void eraseFirst() noexcept
199 {
200 Q_ASSERT(that()->isMutable());
201 Q_ASSERT(that()->size);
202 ++that()->ptr;
203 --that()->size;
204 }
205
206 void eraseLast() noexcept
207 {
208 Q_ASSERT(that()->isMutable());
209 Q_ASSERT(that()->size);
210 --that()->size;
211 }
212
213 void assign(T *b, T *e, parameter_type t) noexcept
214 {
215 Q_ASSERT(b <= e);
216 Q_ASSERT(b >= that()->begin() && e <= that()->end());
217
218 while (b != e)
219 ::memcpy(static_cast<void *>(b++), static_cast<const void *>(&t), sizeof(T));
220 }
221
222 void reallocate(qsizetype alloc, QArrayData::AllocationOption option)
223 {
224 auto pair = Data::reallocateUnaligned(that()->d, that()->ptr, alloc, option);
225 Q_CHECK_PTR(pair.ptr);
226 Q_ASSERT(pair.header != nullptr);
227 that()->d = pair.header;
228 that()->ptr = pair.ptr;
229 }
230};
231
232template <class T>
234{
235 static_assert (std::is_nothrow_destructible_v<T>, "Types with throwing destructors are not supported in Qt containers.");
236
237protected:
240
241private:
242 DataPointer *m_ptr;
243
244public:
245 explicit QGenericArrayOps(DataPointer &dp) : m_ptr{&dp} {}
246
248 { return m_ptr; }
249 const DataPointer *that() const
250 { return m_ptr; }
251
253
254 void copyAppend(const T *b, const T *e)
255 {
256 Q_ASSERT(that()->isMutable() || b == e);
257 Q_ASSERT(!that()->isShared() || b == e);
258 Q_ASSERT(b <= e);
259 Q_ASSERT((e - b) <= that()->freeSpaceAtEnd());
260
261 if (b == e) // short-cut and handling the case b and e == nullptr
262 return;
263
264 T *data = that()->begin();
265 while (b < e) {
266 new (data + that()->size) T(*b);
267 ++b;
268 ++that()->size;
269 }
270 }
271
272 void copyAppend(qsizetype n, parameter_type t)
273 {
274 Q_ASSERT(!that()->isShared() || n == 0);
275 Q_ASSERT(that()->freeSpaceAtEnd() >= n);
276 if (!n)
277 return;
278
279 T *data = that()->begin();
280 while (n--) {
281 new (data + that()->size) T(t);
282 ++that()->size;
283 }
284 }
285
286 void moveAppend(T *b, T *e)
287 {
288 Q_ASSERT(that()->isMutable() || b == e);
289 Q_ASSERT(!that()->isShared() || b == e);
290 Q_ASSERT(b <= e);
291 Q_ASSERT((e - b) <= that()->freeSpaceAtEnd());
292
293 if (b == e)
294 return;
295
296 T *data = that()->begin();
297 while (b < e) {
298 new (data + that()->size) T(std::move(*b));
299 ++b;
300 ++that()->size;
301 }
302 }
303
304 void truncate(size_t newSize)
305 {
306 Q_ASSERT(that()->isMutable());
307 Q_ASSERT(!that()->isShared());
308 Q_ASSERT(newSize <= size_t(that()->size));
309
310 std::destroy(that()->begin() + newSize, that()->end());
311 that()->size = newSize;
312 }
313
314 void destroyAll() // Call from destructors, ONLY
315 {
316 Q_ASSERT(that()->d);
317 // As this is to be called only from destructor, it doesn't need to be
318 // exception safe; size not updated.
319
320 Q_ASSERT(that()->d->ref_.loadRelaxed() == 0);
321
322 std::destroy(that()->begin(), that()->end());
323 }
324
325 struct Inserter
326 {
330
332 T *end = nullptr, *last = nullptr, *where = nullptr;
333
334 Inserter(QArrayDataPointer<T> *d) : data(d)
335 {
336 begin = d->ptr;
337 size = d->size;
338 }
340 data->ptr = begin;
341 data->size = size;
342 }
344
346 {
347 end = begin + size;
348 last = end - 1;
349 where = begin + pos;
352 nSource = n;
353 move = n - dist; // smaller 0
355 if (n > dist) {
357 move = 0;
359 }
360 }
361
363 {
366
367 setup(pos, n);
368
369 // first create new elements at the end, by copying from elements
370 // to be inserted (if they extend past the current end of the array)
371 for (qsizetype i = 0; i != sourceCopyConstruct; ++i) {
372 new (end + i) T(source[nSource - sourceCopyConstruct + i]);
373 ++size;
374 }
375 Q_ASSERT(size <= oldSize + n);
376
377 // now move construct new elements at the end from existing elements inside
378 // the array.
379 for (qsizetype i = sourceCopyConstruct; i != nSource; ++i) {
380 new (end + i) T(std::move(*(end + i - nSource)));
381 ++size;
382 }
383 // array has the new size now!
384 Q_ASSERT(size == oldSize + n);
385
386 // now move assign existing elements towards the end
387 for (qsizetype i = 0; i != move; --i)
388 last[i] = std::move(last[i - nSource]);
389
390 // finally copy the remaining elements from source over
391 for (qsizetype i = 0; i != sourceCopyAssign; ++i)
392 where[i] = source[i];
393 }
394
396 {
397 const qsizetype oldSize = size;
399
400 setup(pos, n);
401
402 // first create new elements at the end, by copying from elements
403 // to be inserted (if they extend past the current end of the array)
404 for (qsizetype i = 0; i != sourceCopyConstruct; ++i) {
405 new (end + i) T(t);
406 ++size;
407 }
408 Q_ASSERT(size <= oldSize + n);
409
410 // now move construct new elements at the end from existing elements inside
411 // the array.
412 for (qsizetype i = sourceCopyConstruct; i != nSource; ++i) {
413 new (end + i) T(std::move(*(end + i - nSource)));
414 ++size;
415 }
416 // array has the new size now!
417 Q_ASSERT(size == oldSize + n);
418
419 // now move assign existing elements towards the end
420 for (qsizetype i = 0; i != move; --i)
421 last[i] = std::move(last[i - nSource]);
422
423 // finally copy the remaining elements from source over
424 for (qsizetype i = 0; i != sourceCopyAssign; ++i)
425 where[i] = t;
426 }
427
429 {
430 setup(pos, 1);
431
434 new (end) T(std::move(t));
435 ++size;
436 } else {
437 // create a new element at the end by move constructing one existing element
438 // inside the array.
439 new (end) T(std::move(*(end - 1)));
440 ++size;
441
442 // now move assign existing elements towards the end
443 for (qsizetype i = 0; i != move; --i)
444 last[i] = std::move(last[i - 1]);
445
446 // and move the new item into place
447 *where = std::move(t);
448 }
449 }
450 };
451
452 void insert(qsizetype i, const T *data, qsizetype n)
453 {
454 const bool growsAtBegin = that()->size != 0 && i == 0;
455 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
456
457 DataPointer oldData;
458 that()->detachAndGrow(pos, n, &data, &oldData);
459 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
460 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
461
462 if (growsAtBegin) {
463 // copy construct items in reverse order at the begin
464 Q_ASSERT(that()->freeSpaceAtBegin() >= n);
465 while (n) {
466 --n;
467 new (that()->begin() - 1) T(data[n]);
468 --that()->ptr;
469 ++that()->size;
470 }
471 } else {
472 Inserter{that()}.insert(i, data, n);
473 }
474 }
475
476 void insert(qsizetype i, qsizetype n, parameter_type t)
477 {
478 Q_DECL_UNINITIALIZED
479 T copy(t);
480
481 const bool growsAtBegin = that()->size != 0 && i == 0;
482 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
483
484 that()->detachAndGrow(pos, n, nullptr, nullptr);
485 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
486 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
487
488 if (growsAtBegin) {
489 // copy construct items in reverse order at the begin
490 Q_ASSERT(that()->freeSpaceAtBegin() >= n);
491 while (n--) {
492 new (that()->begin() - 1) T(copy);
493 --that()->ptr;
494 ++that()->size;
495 }
496 } else {
497 Inserter{that()}.insert(i, copy, n);
498 }
499 }
500
501 template<typename... Args>
502 void emplace(qsizetype i, Args &&... args)
503 {
504 bool detach = that()->needsDetach();
505 if (!detach) {
506 if (i == that()->size && that()->freeSpaceAtEnd()) {
507 new (that()->end()) T(std::forward<Args>(args)...);
508 ++that()->size;
509 return;
510 }
511 if (i == 0 && that()->freeSpaceAtBegin()) {
512 new (that()->begin() - 1) T(std::forward<Args>(args)...);
513 --that()->ptr;
514 ++that()->size;
515 return;
516 }
517 }
518 Q_DECL_UNINITIALIZED
519 T tmp(std::forward<Args>(args)...);
520 const bool growsAtBegin = that()->size != 0 && i == 0;
521 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
522
523 that()->detachAndGrow(pos, 1, nullptr, nullptr);
524
525 if (growsAtBegin) {
526 Q_ASSERT(that()->freeSpaceAtBegin());
527 new (that()->begin() - 1) T(std::move(tmp));
528 --that()->ptr;
529 ++that()->size;
530 } else {
531 Inserter{that()}.insertOne(i, std::move(tmp));
532 }
533 }
534
535 void erase(T *b, qsizetype n)
536 {
537 T *e = b + n;
538 Q_ASSERT(that()->isMutable());
539 Q_ASSERT(b < e);
540 Q_ASSERT(b >= that()->begin() && b < that()->end());
541 Q_ASSERT(e > that()->begin() && e <= that()->end());
542
543 // Comply with std::vector::erase(): erased elements and all after them
544 // are invalidated. However, erasing from the beginning effectively
545 // means that all iterators are invalidated. We can use this freedom to
546 // erase by moving towards the end.
547 if (b == that()->begin() && e != that()->end()) {
548 that()->ptr = e;
549 } else {
550 const T *const end = that()->end();
551
552 // move (by assignment) the elements from e to end
553 // onto b to the new end
554 while (e != end) {
555 *b = std::move(*e);
556 ++b;
557 ++e;
558 }
559 }
560 that()->size -= n;
561 std::destroy(b, e);
562 }
563
564 void eraseFirst() noexcept
565 {
566 Q_ASSERT(that()->isMutable());
567 Q_ASSERT(that()->size);
568 that()->begin()->~T();
569 ++that()->ptr;
570 --that()->size;
571 }
572
573 void eraseLast() noexcept
574 {
575 Q_ASSERT(that()->isMutable());
576 Q_ASSERT(that()->size);
577 (that()->end() - 1)->~T();
578 --that()->size;
579 }
580
581
582 void assign(T *b, T *e, parameter_type t)
583 {
584 Q_ASSERT(b <= e);
585 Q_ASSERT(b >= that()->begin() && e <= that()->end());
586
587 while (b != e)
588 *b++ = t;
589 }
590};
591
592template <class T>
595{
597 static_assert (std::is_nothrow_destructible_v<T>, "Types with throwing destructors are not supported in Qt containers.");
598
599protected:
602
603public:
604 explicit QMovableArrayOps(DataPointer &dp) : Base(dp) {}
605
607 { return Base::that(); }
608 const DataPointer *that() const
609 { return Base::that(); }
610
611 // using QGenericArrayOps<T>::copyAppend;
612 // using QGenericArrayOps<T>::moveAppend;
613 // using QGenericArrayOps<T>::truncate;
614 // using QGenericArrayOps<T>::destroyAll;
616
617 struct Inserter
618 {
621 T * const displaceTo;
624
626 { Q_ASSERT(displaceFrom == displaceTo); }
627
628 explicit Inserter(QArrayDataPointer<T> *d, qsizetype pos, qsizetype n)
629 : data{d},
630 displaceFrom{d->ptr + pos},
632 nInserts{n},
633 bytes{(data->size - pos) * sizeof(T)}
634 {
635 ::memmove(static_cast<void *>(displaceTo), static_cast<void *>(displaceFrom), bytes);
636 }
638 auto inserts = nInserts;
639 if constexpr (!std::is_nothrow_copy_constructible_v<T>) {
640 if (displaceFrom != displaceTo) {
641 ::memmove(static_cast<void *>(displaceFrom), static_cast<void *>(displaceTo), bytes);
643 }
644 }
645 data->size += inserts;
646 }
648
650 {
651 while (n--) {
652 new (displaceFrom) T(*source);
653 ++source;
654 ++displaceFrom;
655 }
656 verifyPost();
657 }
658
659 void insertFill(const T &t, qsizetype n)
660 {
661 while (n--) {
662 new (displaceFrom) T(t);
663 ++displaceFrom;
664 }
665 verifyPost();
666 }
667
668 void insertOne(T &&t)
669 {
670 new (displaceFrom) T(std::move(t));
671 ++displaceFrom;
672 verifyPost();
673 }
674
675 };
676
677
678 void insert(qsizetype i, const T *data, qsizetype n)
679 {
680 const bool growsAtBegin = that()->size != 0 && i == 0;
681 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
682
683 DataPointer oldData;
684 that()->detachAndGrow(pos, n, &data, &oldData);
685 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
686 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
687
688 if (growsAtBegin) {
689 // copy construct items in reverse order at the begin
690 Q_ASSERT(that()->freeSpaceAtBegin() >= n);
691 while (n) {
692 --n;
693 new (that()->begin() - 1) T(data[n]);
694 --that()->ptr;
695 ++that()->size;
696 }
697 } else {
698 Inserter{that(), i, n}.insertRange(data, n);
699 }
700 }
701
702 void insert(qsizetype i, qsizetype n, parameter_type t)
703 {
704 Q_DECL_UNINITIALIZED
705 T copy(t);
706
707 const bool growsAtBegin = that()->size != 0 && i == 0;
708 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
709
710 that()->detachAndGrow(pos, n, nullptr, nullptr);
711 Q_ASSERT((pos == Data::GrowsAtBeginning && that()->freeSpaceAtBegin() >= n) ||
712 (pos == Data::GrowsAtEnd && that()->freeSpaceAtEnd() >= n));
713
714 if (growsAtBegin) {
715 // copy construct items in reverse order at the begin
716 Q_ASSERT(that()->freeSpaceAtBegin() >= n);
717 while (n--) {
718 new (that()->begin() - 1) T(copy);
719 --that()->ptr;
720 ++that()->size;
721 }
722 } else {
723 Inserter{that(), i, n}.insertFill(copy, n);
724 }
725 }
726
727 template<typename... Args>
728 void emplace(qsizetype i, Args &&... args)
729 {
730 bool detach = that()->needsDetach();
731 if (!detach) {
732 if (i == that()->size && that()->freeSpaceAtEnd()) {
733 new (that()->end()) T(std::forward<Args>(args)...);
734 ++that()->size;
735 return;
736 }
737 if (i == 0 && that()->freeSpaceAtBegin()) {
738 new (that()->begin() - 1) T(std::forward<Args>(args)...);
739 --that()->ptr;
740 ++that()->size;
741 return;
742 }
743 }
744 Q_DECL_UNINITIALIZED
745 T tmp(std::forward<Args>(args)...);
746 const bool growsAtBegin = that()->size != 0 && i == 0;
747 const auto pos = growsAtBegin ? Data::GrowsAtBeginning : Data::GrowsAtEnd;
748
749 that()->detachAndGrow(pos, 1, nullptr, nullptr);
750 if (growsAtBegin) {
751 Q_ASSERT(that()->freeSpaceAtBegin());
752 new (that()->begin() - 1) T(std::move(tmp));
753 --that()->ptr;
754 ++that()->size;
755 } else {
756 Inserter{that(), i, 1}.insertOne(std::move(tmp));
757 }
758 }
759
760 void erase(T *b, qsizetype n)
761 {
762 T *e = b + n;
763
764 Q_ASSERT(that()->isMutable());
765 Q_ASSERT(b < e);
766 Q_ASSERT(b >= that()->begin() && b < that()->end());
767 Q_ASSERT(e > that()->begin() && e <= that()->end());
768
769 // Comply with std::vector::erase(): erased elements and all after them
770 // are invalidated. However, erasing from the beginning effectively
771 // means that all iterators are invalidated. We can use this freedom to
772 // erase by moving towards the end.
773
774 std::destroy(b, e);
775 if (b == that()->begin() && e != that()->end()) {
776 that()->ptr = e;
777 } else if (e != that()->end()) {
778 memmove(static_cast<void *>(b), static_cast<const void *>(e), (static_cast<const T *>(that()->end()) - e)*sizeof(T));
779 }
780 that()->size -= n;
781 }
782
783 void reallocate(qsizetype alloc, QArrayData::AllocationOption option)
784 {
785 auto pair = Data::reallocateUnaligned(that()->d, that()->ptr, alloc, option);
786 Q_CHECK_PTR(pair.ptr);
787 Q_ASSERT(pair.header != nullptr);
788 that()->d = pair.header;
789 that()->ptr = pair.ptr;
790 }
791};
792
793template <class T, class = void>
795{
797};
798
799template <class T>
801 typename std::enable_if<
803 >::type>
804{
806};
807
808template <class T>
810 typename std::enable_if<
812 >::type>
813{
815};
816
817template <class T>
819{
820 using Base = typename QArrayOpsSelector<T>::Type;
824
825protected:
827
828public:
829 using Base::Base;
830
832 { return Base::that(); }
833 const DataPointer *that() const
834 { return Base::that(); }
835
836 // using Base::truncate;
837 // using Base::destroyAll;
838
839 template<typename It>
840 void appendIteratorRange(It b, It e, QtPrivate::IfIsForwardIterator<It> = true)
841 {
842 Q_ASSERT(that()->isMutable() || b == e);
843 Q_ASSERT(!that()->isShared() || b == e);
844 const qsizetype distance = std::distance(b, e);
845 Q_ASSERT(distance >= 0 && distance <= that()->allocatedCapacity() - that()->size);
846 Q_UNUSED(distance);
847
848#if __cplusplus >= 202002L && defined(__cpp_concepts) && defined(__cpp_lib_concepts)
849 constexpr bool canUseCopyAppend =
850 std::contiguous_iterator<It> &&
851 std::is_same_v<
852 std::remove_cv_t<typename std::iterator_traits<It>::value_type>,
853 T
854 >;
855 if constexpr (canUseCopyAppend) {
856 Base::copyAppend(std::to_address(b), std::to_address(e));
857 } else
858#endif
859 {
860 T *iter = that()->end();
861 for (; b != e; ++iter, ++b) {
862 new (iter) T(*b);
863 ++that()->size;
864 }
865 }
866 }
867
868 // slightly higher level API than copyAppend() that also preallocates space
869 void growAppend(const T *b, const T *e)
870 {
871 if (b == e)
872 return;
873 Q_ASSERT(b < e);
874 const qsizetype n = e - b;
875 DataPointer old;
876
877 // points into range:
878 if (QtPrivate::q_points_into_range(b, *that()))
879 that()->detachAndGrow(QArrayData::GrowsAtEnd, n, &b, &old);
880 else
881 that()->detachAndGrow(QArrayData::GrowsAtEnd, n, nullptr, nullptr);
882 Q_ASSERT(that()->freeSpaceAtEnd() >= n);
883 // b might be updated so use [b, n)
884 Base::copyAppend(b, b + n);
885 }
886
887 void appendUninitialized(qsizetype newSize)
888 {
889 Q_ASSERT(that()->isMutable());
890 Q_ASSERT(!that()->isShared());
891 Q_ASSERT(newSize > that()->size);
892 Q_ASSERT(newSize - that()->size <= that()->freeSpaceAtEnd());
893
894
895 T *const b = that()->begin() + that()->size;
896 T *const e = that()->begin() + newSize;
897 if constexpr (std::is_constructible_v<T, Qt::Initialization>)
898 std::uninitialized_fill(b, e, Qt::Uninitialized);
899 else
900 std::uninitialized_default_construct(b, e);
901 that()->size = newSize;
902 }
903
904 using Base::assign;
905
906 template <typename InputIterator, typename Projection = q20::identity>
907 void assign(InputIterator first, InputIterator last, Projection proj = {})
908 {
909 // This function only provides the basic exception guarantee.
910 using Category = typename std::iterator_traits<InputIterator>::iterator_category;
911 constexpr bool IsFwdIt = std::is_convertible_v<Category, std::forward_iterator_tag>;
912
913 const qsizetype n = IsFwdIt ? std::distance(first, last) : 0;
914 bool undoPrependOptimization = true;
915 bool needCapacity = n > that()->constAllocatedCapacity();
916 if (needCapacity || that()->needsDetach()) {
917 qsizetype newCapacity = that()->detachCapacity(n);
918 bool wasLastRef = !that()->deref();
919 if (wasLastRef && needCapacity) {
920 // free memory we can't reuse
921 Base::destroyAll();
922 Data::deallocate(that()->d);
923 }
924 if (!needCapacity && wasLastRef) {
925 // we were the last reference and can reuse the storage
926 that()->d->ref_.storeRelaxed(1);
927 } else {
928 // we must allocate new memory
929 auto [hdr, p] = Data::allocate(newCapacity);
930 that()->d = hdr;
931 that()->ptr = p;
932 that()->size = 0;
933 undoPrependOptimization = false;
934 }
935 }
936
937 if constexpr (!std::is_nothrow_constructible_v<T, decltype(std::invoke(proj, *first))>
938 || !std::is_nothrow_invocable_v<Projection, decltype(*first)>)
939 {
940 // If construction can throw, and we have freeSpaceAtBegin(),
941 // it's easiest to just clear the container and start fresh.
942 // The alternative would be to keep track of two active, disjoint ranges.
943 if (undoPrependOptimization) {
944 Base::truncate(0);
945 that()->setBegin(Data::dataStart(that()->d, alignof(typename Data::AlignmentDummy)));
946 undoPrependOptimization = false;
947 }
948 }
949
950 const auto dend = that()->end();
951 T *dst = that()->begin();
952 T *capacityBegin = dst;
953 if (undoPrependOptimization) {
954 capacityBegin = Data::dataStart(that()->d, alignof(typename Data::AlignmentDummy));
955 that()->setBegin(capacityBegin); // undo prepend optimization
956 }
957
958 assign_impl(first, last, capacityBegin, dst, dend, proj, Category{});
959 }
960
961 template <typename InputIterator, typename Projection>
962 void assign_impl(InputIterator first, InputIterator last, T *capacityBegin, T *dst, T *dend,
963 Projection proj, std::input_iterator_tag)
964 {
965 if (qsizetype offset = dst - capacityBegin) {
966 T *prependBufferEnd = dst;
967 dst = capacityBegin;
968
969 // By construction, the following loop is nothrow!
970 // (otherwise, we can't reach here)
971 // Assumes InputIterator operations don't throw.
972 // (but we can't statically assert that, as these operations
973 // have preconditons, so typically aren't noexcept)
974 while (true) {
975 if (dst == prependBufferEnd) { // ran out of prepend buffer space
976 that()->size += offset;
977 // we now have a contiguous buffer, continue with the main loop:
978 break;
979 }
980 if (first == last) { // ran out of elements to assign
981 std::destroy(prependBufferEnd, dend);
982 that()->size = dst - that()->begin();
983 return;
984 }
985 // construct element in prepend buffer
986 q20::construct_at(dst, std::invoke(proj, *first));
987 ++dst;
988 ++first;
989 }
990 }
991 while (true) {
992 if (first == last) { // ran out of elements to assign
993 std::destroy(dst, dend);
994 break;
995 }
996 if (dst == dend) { // ran out of existing elements to overwrite
997 do {
998 Base::emplace(that()->size, std::invoke(proj, *first));
999 } while (++first != last);
1000 return; // size() is already correct (and dst invalidated)!
1001 }
1002 *dst = std::invoke(proj, *first); // overwrite existing element
1003 ++dst;
1004 ++first;
1005 }
1006 that()->size = dst - that()->begin();
1007 }
1008
1009 template <typename InputIterator, typename Projection>
1010 void assign_impl(InputIterator first, InputIterator last, T *capacityBegin, T *dst, T *dend,
1011 Projection proj, std::forward_iterator_tag)
1012 {
1013 constexpr bool IsIdentity = std::is_same_v<Projection, q20::identity>;
1014 const qsizetype n = std::distance(first, last);
1015 if constexpr (IsIdentity && !QTypeInfo<T>::isComplex) {
1016 // For non-complex types, we prefer a single std::copy() -> memcpy()
1017 // call. We can do that because either the default constructor is
1018 // trivial (so the lifetime has started) or the copy constructor is
1019 // (and won't care what the stored value is).
1020 std::copy(first, last, capacityBegin);
1021 } else {
1022 // There are two possibilities:
1023 // 1) fewer elements than the current allocated space
1024 // | prepend buffer | array | destroy |
1025 // 2) more elements than the current allocated space
1026 // | prepend buffer | array | construct |
1027 //
1028 // Both the prepend buffer and the current array may be empty.
1029
1030 // construct elements in the prepend buffer
1031 while (first != last && capacityBegin != dst) {
1032 q20::construct_at(capacityBegin, std::invoke(proj, *first));
1033 ++first;
1034 ++capacityBegin;
1035 }
1036
1037 // overwrite elements in the existing array
1038 while (first != last && dst != dend) {
1039 *dst = std::invoke(proj, *first); // overwrite existing element
1040 ++first;
1041 ++dst;
1042 }
1043
1044 // construct new elements in the append buffer
1045 while (first != last) {
1046 q20::construct_at(dst, std::invoke(proj, *first));
1047 ++first;
1048 ++dst;
1049 }
1050 // or destroy elements from the existing array
1051 if (dst < dend)
1052 std::destroy(dst, dend);
1053 }
1054 that()->size = n;
1055 }
1056};
1057
1058} // namespace QtPrivate
1059
1060template <class T>
1063{
1064private:
1065 using Base = QtPrivate::QCommonArrayOps<T>;
1066public:
1067 using Base::Base;
1068
1069 QArrayDataOps *operator->() noexcept { return this; }
1070 const QArrayDataOps *operator->() const noexcept { return this; }
1071};
1072
1073QT_END_NAMESPACE
1074
1075#endif // include guard
const QArrayDataOps * operator->() const noexcept
QArrayDataOps * operator->() noexcept
QGenericArrayOps< T > Type
const DataPointer * that() const
void appendUninitialized(qsizetype newSize)
void assign_impl(InputIterator first, InputIterator last, T *capacityBegin, T *dst, T *dend, Projection proj, std::input_iterator_tag)
QCommonArrayOps< T > Self
void assign_impl(InputIterator first, InputIterator last, T *capacityBegin, T *dst, T *dend, Projection proj, std::forward_iterator_tag)
void assign(InputIterator first, InputIterator last, Projection proj={})
void appendIteratorRange(It b, It e, QtPrivate::IfIsForwardIterator< It >=true)
void growAppend(const T *b, const T *e)
void truncate(size_t newSize)
void insert(qsizetype i, qsizetype n, parameter_type t)
QArrayDataPointer< T >::parameter_type parameter_type
QTypedArrayData< T > Data
void emplace(qsizetype i, Args &&... args)
void copyAppend(const T *b, const T *e)
const DataPointer * that() const
void assign(T *b, T *e, parameter_type t)
void erase(T *b, qsizetype n)
void insert(qsizetype i, const T *data, qsizetype n)
void copyAppend(qsizetype n, parameter_type t)
QGenericArrayOps(DataPointer &dp)
QArrayDataPointer< T > *const data
QTypedArrayData< T > Data
void reallocate(qsizetype alloc, QArrayData::AllocationOption option)
QGenericArrayOps< T > Base
void insert(qsizetype i, const T *data, qsizetype n)
void emplace(qsizetype i, Args &&... args)
QGenericArrayOps< T >::parameter_type parameter_type
const DataPointer * that() const
void erase(T *b, qsizetype n)
void insert(qsizetype i, qsizetype n, parameter_type t)
QMovableArrayOps(DataPointer &dp)
void moveAppend(T *b, T *e) noexcept
void eraseFirst() noexcept
QArrayDataPointer< T >::parameter_type parameter_type
void erase(T *b, qsizetype n)
void eraseLast() noexcept
T * createHole(QArrayData::GrowthPosition pos, qsizetype where, qsizetype n)
void insert(qsizetype i, const T *data, qsizetype n)
void truncate(size_t newSize) noexcept
void emplace(qsizetype i, Args &&... args)
void assign(T *b, T *e, parameter_type t) noexcept
const DataPointer * that() const
void copyAppend(const T *b, const T *e) noexcept
void copyAppend(qsizetype n, parameter_type t) noexcept
void reallocate(qsizetype alloc, QArrayData::AllocationOption option)
void insert(qsizetype i, qsizetype n, parameter_type t)
QPodArrayOps(DataPointer &dp)
QTypedArrayData< T > Data
void destroyAll() noexcept