1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QLIST_H
41#define QLIST_H
42
43#include <QtCore/qalgorithms.h>
44#include <QtCore/qiterator.h>
45#include <QtCore/qrefcount.h>
46#include <QtCore/qarraydata.h>
47#include <QtCore/qhashfunctions.h>
48#include <QtCore/qvector.h>
49#include <QtCore/qcontainertools_impl.h>
50
51#include <algorithm>
52#include <initializer_list>
53#include <iterator>
54#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
55#include <list>
56#endif
57
58#include <stdlib.h>
59#include <new>
60#include <limits.h>
61#include <string.h>
62
63#ifdef Q_CC_MSVC
64#pragma warning( push )
65#pragma warning( disable : 4127 ) // "conditional expression is constant"
66#endif
67
68QT_BEGIN_NAMESPACE
69
70
71template <typename T> class QVector;
72template <typename T> class QSet;
73
74template <typename T> struct QListSpecialMethods
75{
76protected:
77 ~QListSpecialMethods() = default;
78};
79template <> struct QListSpecialMethods<QByteArray>;
80template <> struct QListSpecialMethods<QString>;
81
82struct Q_CORE_EXPORT QListData {
83 // tags for tag-dispatching of QList implementations,
84 // based on QList's three different memory layouts:
85 struct NotArrayCompatibleLayout {};
86 struct NotIndirectLayout {};
87 struct ArrayCompatibleLayout : NotIndirectLayout {}; // data laid out like a C array
88 struct InlineWithPaddingLayout : NotArrayCompatibleLayout, NotIndirectLayout {}; // data laid out like a C array with padding
89 struct IndirectLayout : NotArrayCompatibleLayout {}; // data allocated on the heap
90
91 struct Data {
92 QtPrivate::RefCount ref;
93 int alloc, begin, end;
94 void *array[1];
95 };
96 enum { DataHeaderSize = sizeof(Data) - sizeof(void *) };
97
98 Data *detach(int alloc);
99 Data *detach_grow(int *i, int n);
100 void realloc(int alloc);
101 void realloc_grow(int growth);
102 inline void dispose() { dispose(d); }
103 static void dispose(Data *d);
104 static const Data shared_null;
105 Data *d;
106 void **erase(void **xi);
107 void **append(int n);
108 void **append();
109 void **append(const QListData &l);
110 void **prepend();
111 void **insert(int i);
112 void remove(int i);
113 void remove(int i, int n);
114 void move(int from, int to);
115 inline int size() const noexcept { return d->end - d->begin; }
116 inline bool isEmpty() const noexcept { return d->end == d->begin; }
117 inline void **at(int i) const noexcept { return d->array + d->begin + i; }
118 inline void **begin() const noexcept { return d->array + d->begin; }
119 inline void **end() const noexcept { return d->array + d->end; }
120};
121
122namespace QtPrivate {
123 template <typename V, typename U> int indexOf(const QList<V> &list, const U &u, int from);
124 template <typename V, typename U> int lastIndexOf(const QList<V> &list, const U &u, int from);
125}
126
127template <typename T>
128class QList
129#ifndef Q_QDOC
130 : public QListSpecialMethods<T>
131#endif
132{
133public:
134 struct MemoryLayout
135 : std::conditional<
136 // must stay isStatic until ### Qt 6 for BC reasons (don't use !isRelocatable)!
137 QTypeInfo<T>::isStatic || QTypeInfo<T>::isLarge,
138 QListData::IndirectLayout,
139 typename std::conditional<
140 sizeof(T) == sizeof(void*),
141 QListData::ArrayCompatibleLayout,
142 QListData::InlineWithPaddingLayout
143 >::type>::type {};
144private:
145 template <typename V, typename U> friend int QtPrivate::indexOf(const QList<V> &list, const U &u, int from);
146 template <typename V, typename U> friend int QtPrivate::lastIndexOf(const QList<V> &list, const U &u, int from);
147 struct Node { void *v;
148#if defined(Q_CC_BOR)
149 Q_INLINE_TEMPLATE T &t();
150#else
151 Q_INLINE_TEMPLATE T &t()
152 { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic
153 ? v : this); }
154#endif
155 };
156
157 union { QListData p; QListData::Data *d; };
158
159public:
160 inline QList() noexcept : d(const_cast<QListData::Data *>(&QListData::shared_null)) { }
161 QList(const QList<T> &l);
162 ~QList();
163 QList<T> &operator=(const QList<T> &l);
164 inline QList(QList<T> &&other) noexcept
165 : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); }
166 inline QList &operator=(QList<T> &&other) noexcept
167 { QList moved(std::move(other)); swap(moved); return *this; }
168 inline void swap(QList<T> &other) noexcept { qSwap(d, other.d); }
169 inline QList(std::initializer_list<T> args)
170 : QList(args.begin(), args.end()) {}
171 template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true>
172 QList(InputIterator first, InputIterator last);
173 bool operator==(const QList<T> &l) const;
174 inline bool operator!=(const QList<T> &l) const { return !(*this == l); }
175
176 inline int size() const noexcept { return p.size(); }
177
178 inline void detach() { if (d->ref.isShared()) detach_helper(); }
179
180 inline void detachShared()
181 {
182 // The "this->" qualification is needed for GCCE.
183 if (d->ref.isShared() && this->d != &QListData::shared_null)
184 detach_helper();
185 }
186
187 inline bool isDetached() const { return !d->ref.isShared(); }
188#if !defined(QT_NO_UNSHARABLE_CONTAINERS)
189 inline void setSharable(bool sharable)
190 {
191 if (sharable == d->ref.isSharable())
192 return;
193 if (!sharable)
194 detach();
195 if (d != &QListData::shared_null)
196 d->ref.setSharable(sharable);
197 }
198#endif
199 inline bool isSharedWith(const QList<T> &other) const noexcept { return d == other.d; }
200
201 inline bool isEmpty() const noexcept { return p.isEmpty(); }
202
203 void clear();
204
205 const T &at(int i) const;
206 const T &operator[](int i) const;
207 T &operator[](int i);
208
209 void reserve(int size);
210 void append(const T &t);
211 void append(const QList<T> &t);
212 void prepend(const T &t);
213 void insert(int i, const T &t);
214 void replace(int i, const T &t);
215 void removeAt(int i);
216 int removeAll(const T &t);
217 bool removeOne(const T &t);
218 T takeAt(int i);
219 T takeFirst();
220 T takeLast();
221 void move(int from, int to);
222 void swapItemsAt(int i, int j);
223#if QT_DEPRECATED_SINCE(5, 13) && QT_VERSION < QT_VERSION_CHECK(6,0,0)
224 QT_DEPRECATED_X("Use QList<T>::swapItemsAt()")
225 void swap(int i, int j) { swapItemsAt(i, j); }
226#endif
227 int indexOf(const T &t, int from = 0) const;
228 int lastIndexOf(const T &t, int from = -1) const;
229 bool contains(const T &t) const;
230 int count(const T &t) const;
231
232 class const_iterator;
233
234 class iterator {
235 public:
236 Node *i;
237 typedef std::random_access_iterator_tag iterator_category;
238 // ### Qt6: use int
239 typedef qptrdiff difference_type;
240 typedef T value_type;
241 typedef T *pointer;
242 typedef T &reference;
243
244 inline iterator() noexcept : i(nullptr) {}
245 inline iterator(Node *n) noexcept : i(n) {}
246#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
247 // can't remove it in Qt 5, since doing so would make the type trivial,
248 // which changes the way it's passed to functions by value.
249 inline iterator(const iterator &o) noexcept : i(o.i){}
250 inline iterator &operator=(const iterator &o) noexcept
251 { i = o.i; return *this; }
252#endif
253 inline T &operator*() const { return i->t(); }
254 inline T *operator->() const { return &i->t(); }
255 inline T &operator[](difference_type j) const { return i[j].t(); }
256 inline bool operator==(const iterator &o) const noexcept { return i == o.i; }
257 inline bool operator!=(const iterator &o) const noexcept { return i != o.i; }
258 inline bool operator<(const iterator& other) const noexcept { return i < other.i; }
259 inline bool operator<=(const iterator& other) const noexcept { return i <= other.i; }
260 inline bool operator>(const iterator& other) const noexcept { return i > other.i; }
261 inline bool operator>=(const iterator& other) const noexcept { return i >= other.i; }
262#ifndef QT_STRICT_ITERATORS
263 inline bool operator==(const const_iterator &o) const noexcept
264 { return i == o.i; }
265 inline bool operator!=(const const_iterator &o) const noexcept
266 { return i != o.i; }
267 inline bool operator<(const const_iterator& other) const noexcept
268 { return i < other.i; }
269 inline bool operator<=(const const_iterator& other) const noexcept
270 { return i <= other.i; }
271 inline bool operator>(const const_iterator& other) const noexcept
272 { return i > other.i; }
273 inline bool operator>=(const const_iterator& other) const noexcept
274 { return i >= other.i; }
275#endif
276 inline iterator &operator++() { ++i; return *this; }
277 inline iterator operator++(int) { Node *n = i; ++i; return n; }
278 inline iterator &operator--() { i--; return *this; }
279 inline iterator operator--(int) { Node *n = i; i--; return n; }
280 inline iterator &operator+=(difference_type j) { i+=j; return *this; }
281 inline iterator &operator-=(difference_type j) { i-=j; return *this; }
282 inline iterator operator+(difference_type j) const { return iterator(i+j); }
283 inline iterator operator-(difference_type j) const { return iterator(i-j); }
284 friend inline iterator operator+(difference_type j, iterator k) { return k + j; }
285 inline int operator-(iterator j) const { return int(i - j.i); }
286 };
287 friend class iterator;
288
289 class const_iterator {
290 public:
291 Node *i;
292 typedef std::random_access_iterator_tag iterator_category;
293 // ### Qt6: use int
294 typedef qptrdiff difference_type;
295 typedef T value_type;
296 typedef const T *pointer;
297 typedef const T &reference;
298
299 inline const_iterator() noexcept : i(nullptr) {}
300 inline const_iterator(Node *n) noexcept : i(n) {}
301#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
302 // can't remove it in Qt 5, since doing so would make the type trivial,
303 // which changes the way it's passed to functions by value.
304 inline const_iterator(const const_iterator &o) noexcept : i(o.i) {}
305 inline const_iterator &operator=(const const_iterator &o) noexcept
306 { i = o.i; return *this; }
307#endif
308#ifdef QT_STRICT_ITERATORS
309 inline explicit const_iterator(const iterator &o) noexcept : i(o.i) {}
310#else
311 inline const_iterator(const iterator &o) noexcept : i(o.i) {}
312#endif
313 inline const T &operator*() const { return i->t(); }
314 inline const T *operator->() const { return &i->t(); }
315 inline const T &operator[](difference_type j) const { return i[j].t(); }
316 inline bool operator==(const const_iterator &o) const noexcept { return i == o.i; }
317 inline bool operator!=(const const_iterator &o) const noexcept { return i != o.i; }
318 inline bool operator<(const const_iterator& other) const noexcept { return i < other.i; }
319 inline bool operator<=(const const_iterator& other) const noexcept { return i <= other.i; }
320 inline bool operator>(const const_iterator& other) const noexcept { return i > other.i; }
321 inline bool operator>=(const const_iterator& other) const noexcept { return i >= other.i; }
322 inline const_iterator &operator++() { ++i; return *this; }
323 inline const_iterator operator++(int) { Node *n = i; ++i; return n; }
324 inline const_iterator &operator--() { i--; return *this; }
325 inline const_iterator operator--(int) { Node *n = i; i--; return n; }
326 inline const_iterator &operator+=(difference_type j) { i+=j; return *this; }
327 inline const_iterator &operator-=(difference_type j) { i-=j; return *this; }
328 inline const_iterator operator+(difference_type j) const { return const_iterator(i+j); }
329 inline const_iterator operator-(difference_type j) const { return const_iterator(i-j); }
330 friend inline const_iterator operator+(difference_type j, const_iterator k) { return k + j; }
331 inline int operator-(const_iterator j) const { return int(i - j.i); }
332 };
333 friend class const_iterator;
334
335 // stl style
336 typedef std::reverse_iterator<iterator> reverse_iterator;
337 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
338 inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); }
339 inline const_iterator begin() const noexcept { return reinterpret_cast<Node *>(p.begin()); }
340 inline const_iterator cbegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); }
341 inline const_iterator constBegin() const noexcept { return reinterpret_cast<Node *>(p.begin()); }
342 inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); }
343 inline const_iterator end() const noexcept { return reinterpret_cast<Node *>(p.end()); }
344 inline const_iterator cend() const noexcept { return reinterpret_cast<Node *>(p.end()); }
345 inline const_iterator constEnd() const noexcept { return reinterpret_cast<Node *>(p.end()); }
346 reverse_iterator rbegin() { return reverse_iterator(end()); }
347 reverse_iterator rend() { return reverse_iterator(begin()); }
348 const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
349 const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
350 const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
351 const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
352 iterator insert(iterator before, const T &t);
353 iterator erase(iterator pos);
354 iterator erase(iterator first, iterator last);
355
356 // more Qt
357 typedef iterator Iterator;
358 typedef const_iterator ConstIterator;
359 inline int count() const { return p.size(); }
360 inline int length() const { return p.size(); } // Same as count()
361 inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
362 inline const T& constFirst() const { return first(); }
363 inline const T& first() const { Q_ASSERT(!isEmpty()); return at(0); }
364 T& last() { Q_ASSERT(!isEmpty()); return *(--end()); }
365 const T& last() const { Q_ASSERT(!isEmpty()); return at(count() - 1); }
366 inline const T& constLast() const { return last(); }
367 inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); }
368 inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
369 inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
370 inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
371 QList<T> mid(int pos, int length = -1) const;
372
373 T value(int i) const;
374 T value(int i, const T &defaultValue) const;
375
376 // stl compatibility
377 inline void push_back(const T &t) { append(t); }
378 inline void push_front(const T &t) { prepend(t); }
379 inline T& front() { return first(); }
380 inline const T& front() const { return first(); }
381 inline T& back() { return last(); }
382 inline const T& back() const { return last(); }
383 inline void pop_front() { removeFirst(); }
384 inline void pop_back() { removeLast(); }
385 inline bool empty() const { return isEmpty(); }
386 typedef int size_type;
387 typedef T value_type;
388 typedef value_type *pointer;
389 typedef const value_type *const_pointer;
390 typedef value_type &reference;
391 typedef const value_type &const_reference;
392 // ### Qt6: use int
393 typedef qptrdiff difference_type;
394
395 // comfort
396 QList<T> &operator+=(const QList<T> &l);
397 inline QList<T> operator+(const QList<T> &l) const
398 { QList n = *this; n += l; return n; }
399 inline QList<T> &operator+=(const T &t)
400 { append(t); return *this; }
401 inline QList<T> &operator<< (const T &t)
402 { append(t); return *this; }
403 inline QList<T> &operator<<(const QList<T> &l)
404 { *this += l; return *this; }
405
406 static QList<T> fromVector(const QVector<T> &vector);
407 QVector<T> toVector() const;
408
409#if QT_VERSION < QT_VERSION_CHECK(6,0,0)
410 Q_DECL_DEPRECATED_X("Use QList<T>(set.begin(), set.end()) instead.")
411 static QList<T> fromSet(const QSet<T> &set);
412 Q_DECL_DEPRECATED_X("Use QSet<T>(list.begin(), list.end()) instead.")
413 QSet<T> toSet() const;
414
415 Q_DECL_DEPRECATED_X("Use QList<T>(list.begin(), list.end()) instead.")
416 static inline QList<T> fromStdList(const std::list<T> &list)
417 { return QList<T>(list.begin(), list.end()); }
418 Q_DECL_DEPRECATED_X("Use std::list<T>(list.begin(), list.end()) instead.")
419 inline std::list<T> toStdList() const
420 { return std::list<T>(begin(), end()); }
421#endif
422
423private:
424 Node *detach_helper_grow(int i, int n);
425 void detach_helper(int alloc);
426 void detach_helper();
427 void dealloc(QListData::Data *d);
428
429 void node_construct(Node *n, const T &t);
430 void node_destruct(Node *n);
431 void node_copy(Node *from, Node *to, Node *src);
432 void node_destruct(Node *from, Node *to);
433
434 bool isValidIterator(const iterator &i) const noexcept
435 {
436 const std::less<const Node *> less = {};
437 return !less(i.i, cbegin().i) && !less(cend().i, i.i);
438 }
439
440private:
441 inline bool op_eq_impl(const QList &other, QListData::NotArrayCompatibleLayout) const;
442 inline bool op_eq_impl(const QList &other, QListData::ArrayCompatibleLayout) const;
443 inline bool contains_impl(const T &, QListData::NotArrayCompatibleLayout) const;
444 inline bool contains_impl(const T &, QListData::ArrayCompatibleLayout) const;
445 inline int count_impl(const T &, QListData::NotArrayCompatibleLayout) const;
446 inline int count_impl(const T &, QListData::ArrayCompatibleLayout) const;
447};
448
449#if defined(Q_CC_BOR)
450template <typename T>
451Q_INLINE_TEMPLATE T &QList<T>::Node::t()
452{ return QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic ? *(T*)v:*(T*)this; }
453#endif
454
455template <typename T>
456Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
457{
458 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
459 else if (QTypeInfo<T>::isComplex) new (n) T(t);
460#if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__)
461 // This violates pointer aliasing rules, but it is known to be safe (and silent)
462 // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which
463 // set the same define are assumed to be safe.
464 else *reinterpret_cast<T*>(n) = t;
465#else
466 // This is always safe, but penaltizes unoptimized builds a lot.
467 else ::memcpy(n, static_cast<const void *>(&t), sizeof(T));
468#endif
469}
470
471template <typename T>
472Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n)
473{
474 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v);
475 else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T();
476}
477
478template <typename T>
479Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src)
480{
481 Node *current = from;
482 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
483 QT_TRY {
484 while(current != to) {
485 current->v = new T(*reinterpret_cast<T*>(src->v));
486 ++current;
487 ++src;
488 }
489 } QT_CATCH(...) {
490 while (current-- != from)
491 delete reinterpret_cast<T*>(current->v);
492 QT_RETHROW;
493 }
494
495 } else if (QTypeInfo<T>::isComplex) {
496 QT_TRY {
497 while(current != to) {
498 new (current) T(*reinterpret_cast<T*>(src));
499 ++current;
500 ++src;
501 }
502 } QT_CATCH(...) {
503 while (current-- != from)
504 (reinterpret_cast<T*>(current))->~T();
505 QT_RETHROW;
506 }
507 } else {
508 if (src != from && to - from > 0)
509 memcpy(from, src, (to - from) * sizeof(Node));
510 }
511}
512
513template <typename T>
514Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to)
515{
516 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic)
517 while(from != to) --to, delete reinterpret_cast<T*>(to->v);
518 else if (QTypeInfo<T>::isComplex)
519 while (from != to) --to, reinterpret_cast<T*>(to)->~T();
520}
521
522template <typename T>
523Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l)
524{
525 if (d != l.d) {
526 QList<T> tmp(l);
527 tmp.swap(*this);
528 }
529 return *this;
530}
531template <typename T>
532inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t)
533{
534 Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid");
535
536 int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin()));
537 Node *n = nullptr;
538 if (d->ref.isShared())
539 n = detach_helper_grow(iBefore, 1);
540 else
541 n = reinterpret_cast<Node *>(p.insert(iBefore));
542 QT_TRY {
543 node_construct(n, t);
544 } QT_CATCH(...) {
545 p.remove(iBefore);
546 QT_RETHROW;
547 }
548 return n;
549}
550template <typename T>
551inline typename QList<T>::iterator QList<T>::erase(iterator it)
552{
553 Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid");
554 if (d->ref.isShared()) {
555 int offset = int(it.i - reinterpret_cast<Node *>(p.begin()));
556 it = begin(); // implies detach()
557 it += offset;
558 }
559 node_destruct(it.i);
560 return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i)));
561}
562template <typename T>
563inline const T &QList<T>::at(int i) const
564{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
565 return reinterpret_cast<Node *>(p.at(i))->t(); }
566template <typename T>
567inline const T &QList<T>::operator[](int i) const
568{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
569 return reinterpret_cast<Node *>(p.at(i))->t(); }
570template <typename T>
571inline T &QList<T>::operator[](int i)
572{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
573 detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }
574template <typename T>
575inline void QList<T>::removeAt(int i)
576{ if(i >= 0 && i < p.size()) { detach();
577 node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); } }
578template <typename T>
579inline T QList<T>::takeAt(int i)
580{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range");
581 detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = std::move(n->t()); node_destruct(n);
582 p.remove(i); return t; }
583template <typename T>
584inline T QList<T>::takeFirst()
585{ T t = std::move(first()); removeFirst(); return t; }
586template <typename T>
587inline T QList<T>::takeLast()
588{ T t = std::move(last()); removeLast(); return t; }
589
590template <typename T>
591Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc)
592{
593 if (d->alloc < alloc) {
594 if (d->ref.isShared())
595 detach_helper(alloc);
596 else
597 p.realloc(alloc);
598 }
599}
600
601template <typename T>
602Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t)
603{
604 if (d->ref.isShared()) {
605 Node *n = detach_helper_grow(INT_MAX, 1);
606 QT_TRY {
607 node_construct(n, t);
608 } QT_CATCH(...) {
609 --d->end;
610 QT_RETHROW;
611 }
612 } else {
613 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
614 Node *n = reinterpret_cast<Node *>(p.append());
615 QT_TRY {
616 node_construct(n, t);
617 } QT_CATCH(...) {
618 --d->end;
619 QT_RETHROW;
620 }
621 } else {
622 Node *n, copy;
623 node_construct(&copy, t); // t might be a reference to an object in the array
624 QT_TRY {
625 n = reinterpret_cast<Node *>(p.append());;
626 } QT_CATCH(...) {
627 node_destruct(&copy);
628 QT_RETHROW;
629 }
630 *n = copy;
631 }
632 }
633}
634
635template <typename T>
636inline void QList<T>::prepend(const T &t)
637{
638 if (d->ref.isShared()) {
639 Node *n = detach_helper_grow(0, 1);
640 QT_TRY {
641 node_construct(n, t);
642 } QT_CATCH(...) {
643 ++d->begin;
644 QT_RETHROW;
645 }
646 } else {
647 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
648 Node *n = reinterpret_cast<Node *>(p.prepend());
649 QT_TRY {
650 node_construct(n, t);
651 } QT_CATCH(...) {
652 ++d->begin;
653 QT_RETHROW;
654 }
655 } else {
656 Node *n, copy;
657 node_construct(&copy, t); // t might be a reference to an object in the array
658 QT_TRY {
659 n = reinterpret_cast<Node *>(p.prepend());;
660 } QT_CATCH(...) {
661 node_destruct(&copy);
662 QT_RETHROW;
663 }
664 *n = copy;
665 }
666 }
667}
668
669template <typename T>
670inline void QList<T>::insert(int i, const T &t)
671{
672 if (d->ref.isShared()) {
673 Node *n = detach_helper_grow(i, 1);
674 QT_TRY {
675 node_construct(n, t);
676 } QT_CATCH(...) {
677 p.remove(i);
678 QT_RETHROW;
679 }
680 } else {
681 if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
682 Node *n = reinterpret_cast<Node *>(p.insert(i));
683 QT_TRY {
684 node_construct(n, t);
685 } QT_CATCH(...) {
686 p.remove(i);
687 QT_RETHROW;
688 }
689 } else {
690 Node *n, copy;
691 node_construct(&copy, t); // t might be a reference to an object in the array
692 QT_TRY {
693 n = reinterpret_cast<Node *>(p.insert(i));;
694 } QT_CATCH(...) {
695 node_destruct(&copy);
696 QT_RETHROW;
697 }
698 *n = copy;
699 }
700 }
701}
702
703template <typename T>
704inline void QList<T>::replace(int i, const T &t)
705{
706 Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range");
707 detach();
708 reinterpret_cast<Node *>(p.at(i))->t() = t;
709}
710
711template <typename T>
712inline void QList<T>::swapItemsAt(int i, int j)
713{
714 Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(),
715 "QList<T>::swap", "index out of range");
716 detach();
717 qSwap(d->array[d->begin + i], d->array[d->begin + j]);
718}
719
720template <typename T>
721inline void QList<T>::move(int from, int to)
722{
723 Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(),
724 "QList<T>::move", "index out of range");
725 detach();
726 p.move(from, to);
727}
728
729template<typename T>
730Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int alength) const
731{
732 using namespace QtPrivate;
733 switch (QContainerImplHelper::mid(size(), &pos, &alength)) {
734 case QContainerImplHelper::Null:
735 case QContainerImplHelper::Empty:
736 return QList<T>();
737 case QContainerImplHelper::Full:
738 return *this;
739 case QContainerImplHelper::Subset:
740 break;
741 }
742
743 QList<T> cpy;
744 if (alength <= 0)
745 return cpy;
746 cpy.reserve(alength);
747 cpy.d->end = alength;
748 QT_TRY {
749 cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()),
750 reinterpret_cast<Node *>(cpy.p.end()),
751 reinterpret_cast<Node *>(p.begin() + pos));
752 } QT_CATCH(...) {
753 // restore the old end
754 cpy.d->end = 0;
755 QT_RETHROW;
756 }
757 return cpy;
758}
759
760template<typename T>
761Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const
762{
763 if (i < 0 || i >= p.size()) {
764 return T();
765 }
766 return reinterpret_cast<Node *>(p.at(i))->t();
767}
768
769template<typename T>
770Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const
771{
772 return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t());
773}
774
775template <typename T>
776Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i, int c)
777{
778 Node *n = reinterpret_cast<Node *>(p.begin());
779 QListData::Data *x = p.detach_grow(&i, c);
780 QT_TRY {
781 node_copy(reinterpret_cast<Node *>(p.begin()),
782 reinterpret_cast<Node *>(p.begin() + i), n);
783 } QT_CATCH(...) {
784 p.dispose();
785 d = x;
786 QT_RETHROW;
787 }
788 QT_TRY {
789 node_copy(reinterpret_cast<Node *>(p.begin() + i + c),
790 reinterpret_cast<Node *>(p.end()), n + i);
791 } QT_CATCH(...) {
792 node_destruct(reinterpret_cast<Node *>(p.begin()),
793 reinterpret_cast<Node *>(p.begin() + i));
794 p.dispose();
795 d = x;
796 QT_RETHROW;
797 }
798
799 if (!x->ref.deref())
800 dealloc(x);
801
802 return reinterpret_cast<Node *>(p.begin() + i);
803}
804
805template <typename T>
806Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(int alloc)
807{
808 Node *n = reinterpret_cast<Node *>(p.begin());
809 QListData::Data *x = p.detach(alloc);
810 QT_TRY {
811 node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n);
812 } QT_CATCH(...) {
813 p.dispose();
814 d = x;
815 QT_RETHROW;
816 }
817
818 if (!x->ref.deref())
819 dealloc(x);
820}
821
822template <typename T>
823Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper()
824{
825 detach_helper(d->alloc);
826}
827
828template <typename T>
829Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l)
830 : QListSpecialMethods<T>(l), d(l.d)
831{
832 if (!d->ref.ref()) {
833 p.detach(d->alloc);
834
835 QT_TRY {
836 node_copy(reinterpret_cast<Node *>(p.begin()),
837 reinterpret_cast<Node *>(p.end()),
838 reinterpret_cast<Node *>(l.p.begin()));
839 } QT_CATCH(...) {
840 QListData::dispose(d);
841 QT_RETHROW;
842 }
843 }
844}
845
846template <typename T>
847Q_OUTOFLINE_TEMPLATE QList<T>::~QList()
848{
849 if (!d->ref.deref())
850 dealloc(d);
851}
852
853template <typename T>
854template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator>>
855QList<T>::QList(InputIterator first, InputIterator last)
856 : QList()
857{
858 QtPrivate::reserveIfForwardIterator(this, first, last);
859 std::copy(first, last, std::back_inserter(*this));
860}
861
862template <typename T>
863Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const
864{
865 if (d == l.d)
866 return true;
867 if (p.size() != l.p.size())
868 return false;
869 return this->op_eq_impl(l, MemoryLayout());
870}
871
872template <typename T>
873inline bool QList<T>::op_eq_impl(const QList &l, QListData::NotArrayCompatibleLayout) const
874{
875 Node *i = reinterpret_cast<Node *>(p.begin());
876 Node *e = reinterpret_cast<Node *>(p.end());
877 Node *li = reinterpret_cast<Node *>(l.p.begin());
878 for (; i != e; ++i, ++li) {
879 if (!(i->t() == li->t()))
880 return false;
881 }
882 return true;
883}
884
885template <typename T>
886inline bool QList<T>::op_eq_impl(const QList &l, QListData::ArrayCompatibleLayout) const
887{
888 const T *lb = reinterpret_cast<const T*>(l.p.begin());
889 const T *b = reinterpret_cast<const T*>(p.begin());
890 const T *e = reinterpret_cast<const T*>(p.end());
891 return std::equal(b, e, QT_MAKE_CHECKED_ARRAY_ITERATOR(lb, l.p.size()));
892}
893
894template <typename T>
895Q_OUTOFLINE_TEMPLATE void QList<T>::dealloc(QListData::Data *data)
896{
897 node_destruct(reinterpret_cast<Node *>(data->array + data->begin),
898 reinterpret_cast<Node *>(data->array + data->end));
899 QListData::dispose(data);
900}
901
902
903template <typename T>
904Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
905{
906 *this = QList<T>();
907}
908
909template <typename T>
910Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t)
911{
912 int index = indexOf(_t);
913 if (index == -1)
914 return 0;
915
916 const T t = _t;
917 detach();
918
919 Node *i = reinterpret_cast<Node *>(p.at(index));
920 Node *e = reinterpret_cast<Node *>(p.end());
921 Node *n = i;
922 node_destruct(i);
923 while (++i != e) {
924 if (i->t() == t)
925 node_destruct(i);
926 else
927 *n++ = *i;
928 }
929
930 int removedCount = int(e - n);
931 d->end -= removedCount;
932 return removedCount;
933}
934
935template <typename T>
936Q_OUTOFLINE_TEMPLATE bool QList<T>::removeOne(const T &_t)
937{
938 int index = indexOf(_t);
939 if (index != -1) {
940 removeAt(index);
941 return true;
942 }
943 return false;
944}
945
946template <typename T>
947Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst,
948 typename QList<T>::iterator alast)
949{
950 Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid");
951 Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid");
952
953 if (d->ref.isShared()) {
954 // ### A block is erased and a detach is needed. We should shrink and only copy relevant items.
955 int offsetfirst = int(afirst.i - reinterpret_cast<Node *>(p.begin()));
956 int offsetlast = int(alast.i - reinterpret_cast<Node *>(p.begin()));
957 afirst = begin(); // implies detach()
958 alast = afirst;
959 afirst += offsetfirst;
960 alast += offsetlast;
961 }
962
963 for (Node *n = afirst.i; n < alast.i; ++n)
964 node_destruct(n);
965 int idx = afirst - begin();
966 p.remove(idx, alast - afirst);
967 return begin() + idx;
968}
969
970template <typename T>
971Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l)
972{
973 if (!l.isEmpty()) {
974 if (d == &QListData::shared_null) {
975 *this = l;
976 } else {
977 Node *n = (d->ref.isShared())
978 ? detach_helper_grow(INT_MAX, l.size())
979 : reinterpret_cast<Node *>(p.append(l.p));
980 QT_TRY {
981 node_copy(n, reinterpret_cast<Node *>(p.end()),
982 reinterpret_cast<Node *>(l.p.begin()));
983 } QT_CATCH(...) {
984 // restore the old end
985 d->end -= int(reinterpret_cast<Node *>(p.end()) - n);
986 QT_RETHROW;
987 }
988 }
989 }
990 return *this;
991}
992
993template <typename T>
994inline void QList<T>::append(const QList<T> &t)
995{
996 *this += t;
997}
998
999template <typename T>
1000Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const
1001{
1002 return QtPrivate::indexOf<T, T>(*this, t, from);
1003}
1004
1005namespace QtPrivate
1006{
1007template <typename T, typename U>
1008int indexOf(const QList<T> &list, const U &u, int from)
1009{
1010 typedef typename QList<T>::Node Node;
1011
1012 if (from < 0)
1013 from = qMax(from + list.p.size(), 0);
1014 if (from < list.p.size()) {
1015 Node *n = reinterpret_cast<Node *>(list.p.at(from -1));
1016 Node *e = reinterpret_cast<Node *>(list.p.end());
1017 while (++n != e)
1018 if (n->t() == u)
1019 return int(n - reinterpret_cast<Node *>(list.p.begin()));
1020 }
1021 return -1;
1022}
1023}
1024
1025template <typename T>
1026Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const
1027{
1028 return QtPrivate::lastIndexOf<T, T>(*this, t, from);
1029}
1030
1031namespace QtPrivate
1032{
1033template <typename T, typename U>
1034int lastIndexOf(const QList<T> &list, const U &u, int from)
1035{
1036 typedef typename QList<T>::Node Node;
1037
1038 if (from < 0)
1039 from += list.p.size();
1040 else if (from >= list.p.size())
1041 from = list.p.size()-1;
1042 if (from >= 0) {
1043 Node *b = reinterpret_cast<Node *>(list.p.begin());
1044 Node *n = reinterpret_cast<Node *>(list.p.at(from + 1));
1045 while (n-- != b) {
1046 if (n->t() == u)
1047 return n - b;
1048 }
1049 }
1050 return -1;
1051}
1052}
1053
1054template <typename T>
1055Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
1056{
1057 return contains_impl(t, MemoryLayout());
1058}
1059
1060template <typename T>
1061inline bool QList<T>::contains_impl(const T &t, QListData::NotArrayCompatibleLayout) const
1062{
1063 Node *e = reinterpret_cast<Node *>(p.end());
1064 Node *i = reinterpret_cast<Node *>(p.begin());
1065 for (; i != e; ++i)
1066 if (i->t() == t)
1067 return true;
1068 return false;
1069}
1070
1071template <typename T>
1072inline bool QList<T>::contains_impl(const T &t, QListData::ArrayCompatibleLayout) const
1073{
1074 const T *b = reinterpret_cast<const T*>(p.begin());
1075 const T *e = reinterpret_cast<const T*>(p.end());
1076 return std::find(b, e, t) != e;
1077}
1078
1079template <typename T>
1080Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const
1081{
1082 return this->count_impl(t, MemoryLayout());
1083}
1084
1085template <typename T>
1086inline int QList<T>::count_impl(const T &t, QListData::NotArrayCompatibleLayout) const
1087{
1088 int c = 0;
1089 Node *e = reinterpret_cast<Node *>(p.end());
1090 Node *i = reinterpret_cast<Node *>(p.begin());
1091 for (; i != e; ++i)
1092 if (i->t() == t)
1093 ++c;
1094 return c;
1095}
1096
1097template <typename T>
1098inline int QList<T>::count_impl(const T &t, QListData::ArrayCompatibleLayout) const
1099{
1100 return int(std::count(reinterpret_cast<const T*>(p.begin()),
1101 reinterpret_cast<const T*>(p.end()),
1102 t));
1103}
1104
1105template <typename T>
1106Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const
1107{
1108 return QVector<T>(begin(), end());
1109}
1110
1111template <typename T>
1112QList<T> QList<T>::fromVector(const QVector<T> &vector)
1113{
1114 return vector.toList();
1115}
1116
1117template <typename T>
1118Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const
1119{
1120 return QList<T>(begin(), end());
1121}
1122
1123template <typename T>
1124QVector<T> QVector<T>::fromList(const QList<T> &list)
1125{
1126 return list.toVector();
1127}
1128
1129Q_DECLARE_SEQUENTIAL_ITERATOR(List)
1130Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)
1131
1132template <typename T>
1133uint qHash(const QList<T> &key, uint seed = 0)
1134 noexcept(noexcept(qHashRange(key.cbegin(), key.cend(), seed)))
1135{
1136 return qHashRange(key.cbegin(), key.cend(), seed);
1137}
1138
1139template <typename T>
1140bool operator<(const QList<T> &lhs, const QList<T> &rhs)
1141 noexcept(noexcept(std::lexicographical_compare(lhs.begin(), lhs.end(),
1142 rhs.begin(), rhs.end())))
1143{
1144 return std::lexicographical_compare(lhs.begin(), lhs.end(),
1145 rhs.begin(), rhs.end());
1146}
1147
1148template <typename T>
1149inline bool operator>(const QList<T> &lhs, const QList<T> &rhs)
1150 noexcept(noexcept(lhs < rhs))
1151{
1152 return rhs < lhs;
1153}
1154
1155template <typename T>
1156inline bool operator<=(const QList<T> &lhs, const QList<T> &rhs)
1157 noexcept(noexcept(lhs < rhs))
1158{
1159 return !(lhs > rhs);
1160}
1161
1162template <typename T>
1163inline bool operator>=(const QList<T> &lhs, const QList<T> &rhs)
1164 noexcept(noexcept(lhs < rhs))
1165{
1166 return !(lhs < rhs);
1167}
1168
1169QT_END_NAMESPACE
1170
1171#include <QtCore/qbytearraylist.h>
1172#include <QtCore/qstringlist.h>
1173
1174#ifdef Q_CC_MSVC
1175#pragma warning( pop )
1176#endif
1177
1178#endif // QLIST_H
1179