Line data Source code
1 : /****************************************************************************
2 : **
3 : ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4 : ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 : ** conditions see http://qt.digia.com/licensing. For further information
15 : ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 : ** Foundation and appearing in the file LICENSE.LGPL included in the
21 : ** packaging of this file. Please review the following information to
22 : ** ensure the GNU Lesser General Public License version 2.1 requirements
23 : ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 : **
25 : ** In addition, as a special exception, Digia gives you certain additional
26 : ** rights. These rights are described in the Digia Qt LGPL Exception
27 : ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 : **
29 : ** GNU General Public License Usage
30 : ** Alternatively, this file may be used under the terms of the GNU
31 : ** General Public License version 3.0 as published by the Free Software
32 : ** Foundation and appearing in the file LICENSE.GPL included in the
33 : ** packaging of this file. Please review the following information to
34 : ** ensure the GNU General Public License version 3.0 requirements will be
35 : ** met: http://www.gnu.org/copyleft/gpl.html.
36 : **
37 : **
38 : ** $QT_END_LICENSE$
39 : **
40 : ****************************************************************************/
41 :
42 : #ifndef QLIST_H
43 : #define QLIST_H
44 :
45 : #include <QtCore/qalgorithms.h>
46 : #include <QtCore/qiterator.h>
47 : #include <QtCore/qrefcount.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 : struct Q_CORE_EXPORT QListData {
73 : struct Data {
74 : QtPrivate::RefCount ref;
75 : int alloc, begin, end;
76 : void *array[1];
77 : };
78 : enum { DataHeaderSize = sizeof(Data) - sizeof(void *) };
79 :
80 : Data *detach(int alloc);
81 : Data *detach_grow(int *i, int n);
82 : void realloc(int alloc);
83 0 : inline void dispose() { dispose(d); }
84 : static void dispose(Data *d);
85 : static const Data shared_null;
86 : Data *d;
87 : void **erase(void **xi);
88 : void **append(int n);
89 : void **append();
90 : void **append(const QListData &l);
91 : void **prepend();
92 : void **insert(int i);
93 : void remove(int i);
94 : void remove(int i, int n);
95 : void move(int from, int to);
96 46 : inline int size() const { return d->end - d->begin; }
97 0 : inline bool isEmpty() const { return d->end == d->begin; }
98 0 : inline void **at(int i) const { return d->array + d->begin + i; }
99 226 : inline void **begin() const { return d->array + d->begin; }
100 69 : inline void **end() const { return d->array + d->end; }
101 : };
102 :
103 : template <typename T>
104 : class QList
105 : {
106 : struct Node { void *v;
107 : #if defined(Q_CC_BOR)
108 : Q_INLINE_TEMPLATE T &t();
109 : #else
110 39 : Q_INLINE_TEMPLATE T &t()
111 : { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic
112 39 : ? v : this); }
113 : #endif
114 : };
115 :
116 : union { QListData p; QListData::Data *d; };
117 :
118 : public:
119 45 : inline QList() : d(const_cast<QListData::Data *>(&QListData::shared_null)) { }
120 : QList(const QList<T> &l);
121 : ~QList();
122 : QList<T> &operator=(const QList<T> &l);
123 : #ifdef Q_COMPILER_RVALUE_REFS
124 0 : inline QList(QList<T> &&other) : d(other.d) { other.d = const_cast<QListData::Data *>(&QListData::shared_null); }
125 : inline QList &operator=(QList<T> &&other)
126 : { qSwap(d, other.d); return *this; }
127 : #endif
128 0 : inline void swap(QList<T> &other) { qSwap(d, other.d); }
129 : #ifdef Q_COMPILER_INITIALIZER_LISTS
130 0 : inline QList(std::initializer_list<T> args)
131 0 : : d(const_cast<QListData::Data *>(&QListData::shared_null))
132 0 : { std::copy(args.begin(), args.end(), std::back_inserter(*this)); }
133 : #endif
134 : bool operator==(const QList<T> &l) const;
135 : inline bool operator!=(const QList<T> &l) const { return !(*this == l); }
136 :
137 32 : inline int size() const { return p.size(); }
138 :
139 0 : inline void detach() { if (d->ref.isShared()) detach_helper(); }
140 :
141 : inline void detachShared()
142 : {
143 : // The "this->" qualification is needed for GCCE.
144 : if (d->ref.isShared() && this->d != &QListData::shared_null)
145 : detach_helper();
146 : }
147 :
148 : inline bool isDetached() const { return !d->ref.isShared(); }
149 : #if QT_SUPPORTS(UNSHARABLE_CONTAINERS)
150 : inline void setSharable(bool sharable)
151 : {
152 : if (sharable == d->ref.isSharable())
153 : return;
154 : if (!sharable)
155 : detach();
156 : if (d != &QListData::shared_null)
157 : d->ref.setSharable(sharable);
158 : }
159 : #endif
160 : inline bool isSharedWith(const QList<T> &other) const { return d == other.d; }
161 :
162 0 : inline bool isEmpty() const { return p.isEmpty(); }
163 :
164 : void clear();
165 :
166 : const T &at(int i) const;
167 : const T &operator[](int i) const;
168 : T &operator[](int i);
169 :
170 : void reserve(int size);
171 : void append(const T &t);
172 : void append(const QList<T> &t);
173 : void prepend(const T &t);
174 : void insert(int i, const T &t);
175 : void replace(int i, const T &t);
176 : void removeAt(int i);
177 : int removeAll(const T &t);
178 : bool removeOne(const T &t);
179 : T takeAt(int i);
180 : T takeFirst();
181 : T takeLast();
182 : void move(int from, int to);
183 : void swap(int i, int j);
184 : int indexOf(const T &t, int from = 0) const;
185 : int lastIndexOf(const T &t, int from = -1) const;
186 : bool contains(const T &t) const;
187 : int count(const T &t) const;
188 :
189 : class const_iterator;
190 :
191 : class iterator {
192 : public:
193 : Node *i;
194 : typedef std::random_access_iterator_tag iterator_category;
195 : typedef qptrdiff difference_type;
196 : typedef T value_type;
197 : typedef T *pointer;
198 : typedef T &reference;
199 :
200 : inline iterator() : i(0) {}
201 0 : inline iterator(Node *n) : i(n) {}
202 : inline iterator(const iterator &o): i(o.i){}
203 0 : inline T &operator*() const { return i->t(); }
204 : inline T *operator->() const { return &i->t(); }
205 : inline T &operator[](int j) const { return i[j].t(); }
206 : inline bool operator==(const iterator &o) const { return i == o.i; }
207 : inline bool operator!=(const iterator &o) const { return i != o.i; }
208 : inline bool operator<(const iterator& other) const { return i < other.i; }
209 : inline bool operator<=(const iterator& other) const { return i <= other.i; }
210 : inline bool operator>(const iterator& other) const { return i > other.i; }
211 : inline bool operator>=(const iterator& other) const { return i >= other.i; }
212 : #ifndef QT_STRICT_ITERATORS
213 : inline bool operator==(const const_iterator &o) const
214 : { return i == o.i; }
215 : inline bool operator!=(const const_iterator &o) const
216 : { return i != o.i; }
217 : inline bool operator<(const const_iterator& other) const
218 : { return i < other.i; }
219 : inline bool operator<=(const const_iterator& other) const
220 : { return i <= other.i; }
221 : inline bool operator>(const const_iterator& other) const
222 : { return i > other.i; }
223 : inline bool operator>=(const const_iterator& other) const
224 : { return i >= other.i; }
225 : #endif
226 : inline iterator &operator++() { ++i; return *this; }
227 : inline iterator operator++(int) { Node *n = i; ++i; return n; }
228 : inline iterator &operator--() { i--; return *this; }
229 : inline iterator operator--(int) { Node *n = i; i--; return n; }
230 0 : inline iterator &operator+=(int j) { i+=j; return *this; }
231 : inline iterator &operator-=(int j) { i-=j; return *this; }
232 : inline iterator operator+(int j) const { return iterator(i+j); }
233 : inline iterator operator-(int j) const { return iterator(i-j); }
234 : inline int operator-(iterator j) const { return int(i - j.i); }
235 : };
236 : friend class iterator;
237 :
238 : class const_iterator {
239 : public:
240 : Node *i;
241 : typedef std::random_access_iterator_tag iterator_category;
242 : typedef qptrdiff difference_type;
243 : typedef T value_type;
244 : typedef const T *pointer;
245 : typedef const T &reference;
246 :
247 : inline const_iterator() : i(0) {}
248 58 : inline const_iterator(Node *n) : i(n) {}
249 25 : inline const_iterator(const const_iterator &o): i(o.i) {}
250 : #ifdef QT_STRICT_ITERATORS
251 : inline explicit const_iterator(const iterator &o): i(o.i) {}
252 : #else
253 : inline const_iterator(const iterator &o): i(o.i) {}
254 : #endif
255 39 : inline const T &operator*() const { return i->t(); }
256 0 : inline const T *operator->() const { return &i->t(); }
257 : inline const T &operator[](int j) const { return i[j].t(); }
258 : inline bool operator==(const const_iterator &o) const { return i == o.i; }
259 68 : inline bool operator!=(const const_iterator &o) const { return i != o.i; }
260 : inline bool operator<(const const_iterator& other) const { return i < other.i; }
261 : inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
262 : inline bool operator>(const const_iterator& other) const { return i > other.i; }
263 : inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
264 39 : inline const_iterator &operator++() { ++i; return *this; }
265 0 : inline const_iterator operator++(int) { Node *n = i; ++i; return n; }
266 : inline const_iterator &operator--() { i--; return *this; }
267 : inline const_iterator operator--(int) { Node *n = i; i--; return n; }
268 : inline const_iterator &operator+=(int j) { i+=j; return *this; }
269 : inline const_iterator &operator-=(int j) { i-=j; return *this; }
270 : inline const_iterator operator+(int j) const { return const_iterator(i+j); }
271 : inline const_iterator operator-(int j) const { return const_iterator(i-j); }
272 : inline int operator-(const_iterator j) const { return int(i - j.i); }
273 : };
274 : friend class const_iterator;
275 :
276 : // stl style
277 0 : inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); }
278 29 : inline const_iterator begin() const { return reinterpret_cast<Node *>(p.begin()); }
279 : inline const_iterator cbegin() const { return reinterpret_cast<Node *>(p.begin()); }
280 0 : inline const_iterator constBegin() const { return reinterpret_cast<Node *>(p.begin()); }
281 : inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); }
282 29 : inline const_iterator end() const { return reinterpret_cast<Node *>(p.end()); }
283 : inline const_iterator cend() const { return reinterpret_cast<Node *>(p.end()); }
284 0 : inline const_iterator constEnd() const { return reinterpret_cast<Node *>(p.end()); }
285 : iterator insert(iterator before, const T &t);
286 : iterator erase(iterator pos);
287 : iterator erase(iterator first, iterator last);
288 :
289 : // more Qt
290 : typedef iterator Iterator;
291 : typedef const_iterator ConstIterator;
292 14 : inline int count() const { return p.size(); }
293 : inline int length() const { return p.size(); } // Same as count()
294 0 : inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
295 0 : inline const T& first() const { Q_ASSERT(!isEmpty()); return at(0); }
296 : T& last() { Q_ASSERT(!isEmpty()); return *(--end()); }
297 : const T& last() const { Q_ASSERT(!isEmpty()); return at(count() - 1); }
298 0 : inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); }
299 : inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
300 : inline bool startsWith(const T &t) const { return !isEmpty() && first() == t; }
301 : inline bool endsWith(const T &t) const { return !isEmpty() && last() == t; }
302 : QList<T> mid(int pos, int length = -1) const;
303 :
304 : T value(int i) const;
305 : T value(int i, const T &defaultValue) const;
306 :
307 : // stl compatibility
308 0 : inline void push_back(const T &t) { append(t); }
309 : inline void push_front(const T &t) { prepend(t); }
310 0 : inline T& front() { return first(); }
311 0 : inline const T& front() const { return first(); }
312 : inline T& back() { return last(); }
313 : inline const T& back() const { return last(); }
314 0 : inline void pop_front() { removeFirst(); }
315 : inline void pop_back() { removeLast(); }
316 0 : inline bool empty() const { return isEmpty(); }
317 : typedef int size_type;
318 : typedef T value_type;
319 : typedef value_type *pointer;
320 : typedef const value_type *const_pointer;
321 : typedef value_type &reference;
322 : typedef const value_type &const_reference;
323 : typedef qptrdiff difference_type;
324 :
325 : // comfort
326 : QList<T> &operator+=(const QList<T> &l);
327 : inline QList<T> operator+(const QList<T> &l) const
328 : { QList n = *this; n += l; return n; }
329 : inline QList<T> &operator+=(const T &t)
330 : { append(t); return *this; }
331 0 : inline QList<T> &operator<< (const T &t)
332 0 : { append(t); return *this; }
333 : inline QList<T> &operator<<(const QList<T> &l)
334 : { *this += l; return *this; }
335 :
336 : QVector<T> toVector() const;
337 : QSet<T> toSet() const;
338 :
339 : static QList<T> fromVector(const QVector<T> &vector);
340 : static QList<T> fromSet(const QSet<T> &set);
341 :
342 : static inline QList<T> fromStdList(const std::list<T> &list)
343 : { QList<T> tmp; std::copy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
344 : inline std::list<T> toStdList() const
345 : { std::list<T> tmp; std::copy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
346 :
347 : private:
348 : Node *detach_helper_grow(int i, int n);
349 : void detach_helper(int alloc);
350 : void detach_helper();
351 : void dealloc(QListData::Data *d);
352 :
353 : void node_construct(Node *n, const T &t);
354 : void node_destruct(Node *n);
355 : void node_copy(Node *from, Node *to, Node *src);
356 : void node_destruct(Node *from, Node *to);
357 :
358 0 : bool isValidIterator(const iterator &i) const
359 : {
360 0 : return (constBegin().i <= i.i) && (i.i <= constEnd().i);
361 : }
362 : };
363 :
364 : #if defined(Q_CC_BOR)
365 : template <typename T>
366 : Q_INLINE_TEMPLATE T &QList<T>::Node::t()
367 : { return QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic ? *(T*)v:*(T*)this; }
368 : #endif
369 :
370 : template <typename T>
371 58 : Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
372 : {
373 11 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
374 47 : else if (QTypeInfo<T>::isComplex) new (n) T(t);
375 : #if (defined(__GNUC__) || defined(__INTEL_COMPILER) || defined(__IBMCPP__)) && !defined(__OPTIMIZE__)
376 : // This violates pointer aliasing rules, but it is known to be safe (and silent)
377 : // in unoptimized GCC builds (-fno-strict-aliasing). The other compilers which
378 : // set the same define are assumed to be safe.
379 : else *reinterpret_cast<T*>(n) = t;
380 : #else
381 : // This is always safe, but penaltizes unoptimized builds a lot.
382 : else ::memcpy(n, static_cast<const void *>(&t), sizeof(T));
383 : #endif
384 58 : }
385 :
386 : template <typename T>
387 0 : Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n)
388 : {
389 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v);
390 0 : else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T();
391 0 : }
392 :
393 : template <typename T>
394 79 : Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src)
395 : {
396 79 : Node *current = from;
397 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
398 : QT_TRY {
399 2 : while(current != to) {
400 0 : current->v = new T(*reinterpret_cast<T*>(src->v));
401 0 : ++current;
402 0 : ++src;
403 : }
404 0 : } QT_CATCH(...) {
405 0 : while (current-- != from)
406 0 : delete reinterpret_cast<T*>(current->v);
407 0 : QT_RETHROW;
408 : }
409 :
410 : } else if (QTypeInfo<T>::isComplex) {
411 : QT_TRY {
412 156 : while(current != to) {
413 0 : new (current) T(*reinterpret_cast<T*>(src));
414 0 : ++current;
415 0 : ++src;
416 : }
417 0 : } QT_CATCH(...) {
418 0 : while (current-- != from)
419 0 : (reinterpret_cast<T*>(current))->~T();
420 0 : QT_RETHROW;
421 : }
422 : } else {
423 : if (src != from && to - from > 0)
424 : memcpy(from, src, (to - from) * sizeof(Node));
425 : }
426 79 : }
427 :
428 : template <typename T>
429 40 : Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to)
430 : {
431 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic)
432 1 : while(from != to) --to, delete reinterpret_cast<T*>(to->v);
433 : else if (QTypeInfo<T>::isComplex)
434 39 : while (from != to) --to, reinterpret_cast<T*>(to)->~T();
435 40 : }
436 :
437 : template <typename T>
438 0 : Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l)
439 : {
440 0 : if (d != l.d) {
441 0 : QList<T> tmp(l);
442 0 : tmp.swap(*this);
443 : }
444 0 : return *this;
445 : }
446 : template <typename T>
447 : inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t)
448 : {
449 : Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid");
450 :
451 : int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin()));
452 : Node *n = 0;
453 : if (d->ref.isShared())
454 : n = detach_helper_grow(iBefore, 1);
455 : else
456 : n = reinterpret_cast<Node *>(p.insert(iBefore));
457 : QT_TRY {
458 : node_construct(n, t);
459 : } QT_CATCH(...) {
460 : p.remove(iBefore);
461 : QT_RETHROW;
462 : }
463 : return n;
464 : }
465 : template <typename T>
466 0 : inline typename QList<T>::iterator QList<T>::erase(iterator it)
467 : {
468 0 : Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid");
469 0 : if (d->ref.isShared()) {
470 0 : int offset = int(it.i - reinterpret_cast<Node *>(p.begin()));
471 0 : it = begin(); // implies detach()
472 0 : it += offset;
473 : }
474 0 : node_destruct(it.i);
475 0 : return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i)));
476 : }
477 : template <typename T>
478 0 : inline const T &QList<T>::at(int i) const
479 0 : { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
480 0 : return reinterpret_cast<Node *>(p.at(i))->t(); }
481 : template <typename T>
482 : inline const T &QList<T>::operator[](int i) const
483 : { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
484 : return reinterpret_cast<Node *>(p.at(i))->t(); }
485 : template <typename T>
486 : inline T &QList<T>::operator[](int i)
487 : { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
488 : detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }
489 : template <typename T>
490 : inline void QList<T>::removeAt(int i)
491 : { if(i >= 0 && i < p.size()) { detach();
492 : node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); } }
493 : template <typename T>
494 : inline T QList<T>::takeAt(int i)
495 : { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range");
496 : detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = n->t(); node_destruct(n);
497 : p.remove(i); return t; }
498 : template <typename T>
499 : inline T QList<T>::takeFirst()
500 : { T t = first(); removeFirst(); return t; }
501 : template <typename T>
502 : inline T QList<T>::takeLast()
503 : { T t = last(); removeLast(); return t; }
504 :
505 : template <typename T>
506 6 : Q_OUTOFLINE_TEMPLATE void QList<T>::reserve(int alloc)
507 : {
508 6 : if (d->alloc < alloc) {
509 1 : if (d->ref.isShared())
510 1 : detach_helper(alloc);
511 : else
512 0 : p.realloc(alloc);
513 : }
514 6 : }
515 :
516 : template <typename T>
517 58 : Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t)
518 : {
519 58 : if (d->ref.isShared()) {
520 39 : Node *n = detach_helper_grow(INT_MAX, 1);
521 : QT_TRY {
522 39 : node_construct(n, t);
523 0 : } QT_CATCH(...) {
524 0 : --d->end;
525 0 : QT_RETHROW;
526 : }
527 : } else {
528 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
529 11 : Node *n = reinterpret_cast<Node *>(p.append());
530 : QT_TRY {
531 11 : node_construct(n, t);
532 0 : } QT_CATCH(...) {
533 0 : --d->end;
534 0 : QT_RETHROW;
535 : }
536 : } else {
537 : Node *n, copy;
538 8 : node_construct(©, t); // t might be a reference to an object in the array
539 : QT_TRY {
540 8 : n = reinterpret_cast<Node *>(p.append());;
541 0 : } QT_CATCH(...) {
542 0 : node_destruct(©);
543 0 : QT_RETHROW;
544 : }
545 8 : *n = copy;
546 : }
547 : }
548 58 : }
549 :
550 : template <typename T>
551 : inline void QList<T>::prepend(const T &t)
552 : {
553 : if (d->ref.isShared()) {
554 : Node *n = detach_helper_grow(0, 1);
555 : QT_TRY {
556 : node_construct(n, t);
557 : } QT_CATCH(...) {
558 : ++d->begin;
559 : QT_RETHROW;
560 : }
561 : } else {
562 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
563 : Node *n = reinterpret_cast<Node *>(p.prepend());
564 : QT_TRY {
565 : node_construct(n, t);
566 : } QT_CATCH(...) {
567 : ++d->begin;
568 : QT_RETHROW;
569 : }
570 : } else {
571 : Node *n, copy;
572 : node_construct(©, t); // t might be a reference to an object in the array
573 : QT_TRY {
574 : n = reinterpret_cast<Node *>(p.prepend());;
575 : } QT_CATCH(...) {
576 : node_destruct(©);
577 : QT_RETHROW;
578 : }
579 : *n = copy;
580 : }
581 : }
582 : }
583 :
584 : template <typename T>
585 : inline void QList<T>::insert(int i, const T &t)
586 : {
587 : if (d->ref.isShared()) {
588 : Node *n = detach_helper_grow(i, 1);
589 : QT_TRY {
590 : node_construct(n, t);
591 : } QT_CATCH(...) {
592 : p.remove(i);
593 : QT_RETHROW;
594 : }
595 : } else {
596 : if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
597 : Node *n = reinterpret_cast<Node *>(p.insert(i));
598 : QT_TRY {
599 : node_construct(n, t);
600 : } QT_CATCH(...) {
601 : p.remove(i);
602 : QT_RETHROW;
603 : }
604 : } else {
605 : Node *n, copy;
606 : node_construct(©, t); // t might be a reference to an object in the array
607 : QT_TRY {
608 : n = reinterpret_cast<Node *>(p.insert(i));;
609 : } QT_CATCH(...) {
610 : node_destruct(©);
611 : QT_RETHROW;
612 : }
613 : *n = copy;
614 : }
615 : }
616 : }
617 :
618 : template <typename T>
619 : inline void QList<T>::replace(int i, const T &t)
620 : {
621 : Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range");
622 : detach();
623 : reinterpret_cast<Node *>(p.at(i))->t() = t;
624 : }
625 :
626 : template <typename T>
627 : inline void QList<T>::swap(int i, int j)
628 : {
629 : Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(),
630 : "QList<T>::swap", "index out of range");
631 : detach();
632 : void *t = d->array[d->begin + i];
633 : d->array[d->begin + i] = d->array[d->begin + j];
634 : d->array[d->begin + j] = t;
635 : }
636 :
637 : template <typename T>
638 : inline void QList<T>::move(int from, int to)
639 : {
640 : Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(),
641 : "QList<T>::move", "index out of range");
642 : detach();
643 : p.move(from, to);
644 : }
645 :
646 : template<typename T>
647 0 : Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int alength) const
648 : {
649 0 : if (alength < 0 || pos > size() - alength)
650 0 : alength = size() - pos;
651 0 : if (pos == 0 && alength == size())
652 0 : return *this;
653 0 : QList<T> cpy;
654 0 : if (alength <= 0)
655 0 : return cpy;
656 0 : cpy.reserve(alength);
657 0 : cpy.d->end = alength;
658 : QT_TRY {
659 0 : cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()),
660 : reinterpret_cast<Node *>(cpy.p.end()),
661 0 : reinterpret_cast<Node *>(p.begin() + pos));
662 0 : } QT_CATCH(...) {
663 : // restore the old end
664 0 : cpy.d->end = 0;
665 0 : QT_RETHROW;
666 : }
667 0 : return cpy;
668 : }
669 :
670 : template<typename T>
671 : Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const
672 : {
673 : if (i < 0 || i >= p.size()) {
674 : return T();
675 : }
676 : return reinterpret_cast<Node *>(p.at(i))->t();
677 : }
678 :
679 : template<typename T>
680 : Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const
681 : {
682 : return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t());
683 : }
684 :
685 : template <typename T>
686 39 : Q_OUTOFLINE_TEMPLATE typename QList<T>::Node *QList<T>::detach_helper_grow(int i, int c)
687 : {
688 39 : Node *n = reinterpret_cast<Node *>(p.begin());
689 39 : QListData::Data *x = p.detach_grow(&i, c);
690 : QT_TRY {
691 39 : node_copy(reinterpret_cast<Node *>(p.begin()),
692 39 : reinterpret_cast<Node *>(p.begin() + i), n);
693 0 : } QT_CATCH(...) {
694 0 : p.dispose();
695 0 : d = x;
696 0 : QT_RETHROW;
697 : }
698 : QT_TRY {
699 39 : node_copy(reinterpret_cast<Node *>(p.begin() + i + c),
700 39 : reinterpret_cast<Node *>(p.end()), n + i);
701 0 : } QT_CATCH(...) {
702 0 : node_destruct(reinterpret_cast<Node *>(p.begin()),
703 0 : reinterpret_cast<Node *>(p.begin() + i));
704 0 : p.dispose();
705 0 : d = x;
706 0 : QT_RETHROW;
707 : }
708 :
709 39 : if (!x->ref.deref())
710 0 : dealloc(x);
711 :
712 39 : return reinterpret_cast<Node *>(p.begin() + i);
713 : }
714 :
715 : template <typename T>
716 1 : Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(int alloc)
717 : {
718 1 : Node *n = reinterpret_cast<Node *>(p.begin());
719 1 : QListData::Data *x = p.detach(alloc);
720 : QT_TRY {
721 1 : node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n);
722 0 : } QT_CATCH(...) {
723 0 : p.dispose();
724 0 : d = x;
725 0 : QT_RETHROW;
726 : }
727 :
728 1 : if (!x->ref.deref())
729 0 : dealloc(x);
730 1 : }
731 :
732 : template <typename T>
733 0 : Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper()
734 : {
735 0 : detach_helper(d->alloc);
736 0 : }
737 :
738 : template <typename T>
739 42 : Q_OUTOFLINE_TEMPLATE QList<T>::QList(const QList<T> &l)
740 42 : : d(l.d)
741 : {
742 42 : if (!d->ref.ref()) {
743 0 : p.detach(d->alloc);
744 :
745 : QT_TRY {
746 0 : node_copy(reinterpret_cast<Node *>(p.begin()),
747 0 : reinterpret_cast<Node *>(p.end()),
748 0 : reinterpret_cast<Node *>(l.p.begin()));
749 0 : } QT_CATCH(...) {
750 0 : QListData::dispose(d);
751 0 : QT_RETHROW;
752 : }
753 : }
754 42 : }
755 :
756 : template <typename T>
757 91 : Q_OUTOFLINE_TEMPLATE QList<T>::~QList()
758 : {
759 91 : if (!d->ref.deref())
760 40 : dealloc(d);
761 91 : }
762 :
763 : template <typename T>
764 : Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const
765 : {
766 : if (p.size() != l.p.size())
767 : return false;
768 : if (d == l.d)
769 : return true;
770 : Node *i = reinterpret_cast<Node *>(p.end());
771 : Node *b = reinterpret_cast<Node *>(p.begin());
772 : Node *li = reinterpret_cast<Node *>(l.p.end());
773 : while (i != b) {
774 : --i; --li;
775 : if (!(i->t() == li->t()))
776 : return false;
777 : }
778 : return true;
779 : }
780 :
781 : template <typename T>
782 40 : Q_OUTOFLINE_TEMPLATE void QList<T>::dealloc(QListData::Data *data)
783 : {
784 40 : node_destruct(reinterpret_cast<Node *>(data->array + data->begin),
785 40 : reinterpret_cast<Node *>(data->array + data->end));
786 40 : QListData::dispose(data);
787 40 : }
788 :
789 :
790 : template <typename T>
791 : Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
792 : {
793 : *this = QList<T>();
794 : }
795 :
796 : template <typename T>
797 : Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t)
798 : {
799 : int index = indexOf(_t);
800 : if (index == -1)
801 : return 0;
802 :
803 : const T t = _t;
804 : detach();
805 :
806 : Node *i = reinterpret_cast<Node *>(p.at(index));
807 : Node *e = reinterpret_cast<Node *>(p.end());
808 : Node *n = i;
809 : node_destruct(i);
810 : while (++i != e) {
811 : if (i->t() == t)
812 : node_destruct(i);
813 : else
814 : *n++ = *i;
815 : }
816 :
817 : int removedCount = e - n;
818 : d->end -= removedCount;
819 : return removedCount;
820 : }
821 :
822 : template <typename T>
823 : Q_OUTOFLINE_TEMPLATE bool QList<T>::removeOne(const T &_t)
824 : {
825 : int index = indexOf(_t);
826 : if (index != -1) {
827 : removeAt(index);
828 : return true;
829 : }
830 : return false;
831 : }
832 :
833 : template <typename T>
834 : Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst,
835 : typename QList<T>::iterator alast)
836 : {
837 : Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid");
838 : Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid");
839 :
840 : if (d->ref.isShared()) {
841 : // ### A block is erased and a detach is needed. We should shrink and only copy relevant items.
842 : int offsetfirst = int(afirst.i - reinterpret_cast<Node *>(p.begin()));
843 : int offsetlast = int(alast.i - reinterpret_cast<Node *>(p.begin()));
844 : afirst = begin(); // implies detach()
845 : alast = afirst;
846 : afirst += offsetfirst;
847 : alast += offsetlast;
848 : }
849 :
850 : for (Node *n = afirst.i; n < alast.i; ++n)
851 : node_destruct(n);
852 : int idx = afirst - begin();
853 : p.remove(idx, alast - afirst);
854 : return begin() + idx;
855 : }
856 :
857 : template <typename T>
858 : Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l)
859 : {
860 : if (!l.isEmpty()) {
861 : if (isEmpty()) {
862 : *this = l;
863 : } else {
864 : Node *n = (d->ref.isShared())
865 : ? detach_helper_grow(INT_MAX, l.size())
866 : : reinterpret_cast<Node *>(p.append(l.p));
867 : QT_TRY {
868 : node_copy(n, reinterpret_cast<Node *>(p.end()),
869 : reinterpret_cast<Node *>(l.p.begin()));
870 : } QT_CATCH(...) {
871 : // restore the old end
872 : d->end -= int(reinterpret_cast<Node *>(p.end()) - n);
873 : QT_RETHROW;
874 : }
875 : }
876 : }
877 : return *this;
878 : }
879 :
880 : template <typename T>
881 : inline void QList<T>::append(const QList<T> &t)
882 : {
883 : *this += t;
884 : }
885 :
886 : template <typename T>
887 : Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const
888 : {
889 : if (from < 0)
890 : from = qMax(from + p.size(), 0);
891 : if (from < p.size()) {
892 : Node *n = reinterpret_cast<Node *>(p.at(from -1));
893 : Node *e = reinterpret_cast<Node *>(p.end());
894 : while (++n != e)
895 : if (n->t() == t)
896 : return int(n - reinterpret_cast<Node *>(p.begin()));
897 : }
898 : return -1;
899 : }
900 :
901 : template <typename T>
902 : Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const
903 : {
904 : if (from < 0)
905 : from += p.size();
906 : else if (from >= p.size())
907 : from = p.size()-1;
908 : if (from >= 0) {
909 : Node *b = reinterpret_cast<Node *>(p.begin());
910 : Node *n = reinterpret_cast<Node *>(p.at(from + 1));
911 : while (n-- != b) {
912 : if (n->t() == t)
913 : return n - b;
914 : }
915 : }
916 : return -1;
917 : }
918 :
919 : template <typename T>
920 : Q_OUTOFLINE_TEMPLATE bool QList<T>::contains(const T &t) const
921 : {
922 : Node *b = reinterpret_cast<Node *>(p.begin());
923 : Node *i = reinterpret_cast<Node *>(p.end());
924 : while (i-- != b)
925 : if (i->t() == t)
926 : return true;
927 : return false;
928 : }
929 :
930 : template <typename T>
931 : Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const
932 : {
933 : int c = 0;
934 : Node *b = reinterpret_cast<Node *>(p.begin());
935 : Node *i = reinterpret_cast<Node *>(p.end());
936 : while (i-- != b)
937 : if (i->t() == t)
938 : ++c;
939 : return c;
940 : }
941 :
942 : Q_DECLARE_SEQUENTIAL_ITERATOR(List)
943 : Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)
944 :
945 : QT_END_NAMESPACE
946 :
947 : #ifdef Q_CC_MSVC
948 : #pragma warning( pop )
949 : #endif
950 :
951 : #endif // QLIST_H
|