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