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