src/corelib/tools/qlist.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.
00004 **
00005 ** This file is part of the QtCore module of the Qt Toolkit.
00006 **
00007 ** This file may be used under the terms of the GNU General Public
00008 ** License version 2.0 as published by the Free Software Foundation
00009 ** and appearing in the file LICENSE.GPL included in the packaging of
00010 ** this file.  Please review the following information to ensure GNU
00011 ** General Public Licensing requirements will be met:
00012 ** http://www.trolltech.com/products/qt/opensource.html
00013 **
00014 ** If you are unsure which license is appropriate for your use, please
00015 ** review the following information:
00016 ** http://www.trolltech.com/products/qt/licensing.html or contact the
00017 ** sales department at sales@trolltech.com.
00018 **
00019 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00020 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00021 **
00022 ****************************************************************************/
00023 
00024 #ifndef QLIST_H
00025 #define QLIST_H
00026 
00027 #include <QtCore/qiterator.h>
00028 #include <QtCore/qatomic.h>
00029 #include <QtCore/qalgorithms.h>
00030 
00031 #ifndef QT_NO_STL
00032 #include <iterator>
00033 #include <list>
00034 #endif
00035 
00036 #include <new>
00037 
00038 QT_BEGIN_HEADER
00039 
00040 QT_MODULE(Core)
00041 
00042 template <typename T> class QVector;
00043 template <typename T> class QSet;
00044 
00045 struct Q_CORE_EXPORT QListData {
00046     struct Data {
00047         QBasicAtomic ref;
00048         int alloc, begin, end;
00049         uint sharable : 1;
00050         void *array[1];
00051     };
00052     enum { DataHeaderSize = sizeof(Data) - sizeof(void *) };
00053 
00054     Data *detach();
00055     void realloc(int alloc);
00056     static Data shared_null;
00057     Data *d;
00058     void **erase(void **xi);
00059     void **append();
00060     void **append(const QListData &l);
00061     void **prepend();
00062     void **insert(int i);
00063     void remove(int i);
00064     void remove(int i, int n);
00065     void move(int from, int to);
00066     inline int size() const { return d->end - d->begin; }
00067     inline bool isEmpty() const { return d->end  == d->begin; }
00068     inline void **at(int i) const { return d->array + d->begin + i; }
00069     inline void **begin() const { return d->array + d->begin; }
00070     inline void **end() const { return d->array + d->end; }
00071 };
00072 
00073 template <typename T>
00074 class QList
00075 {
00076     struct Node { void *v;
00077 #if defined(Q_CC_BOR)
00078         Q_INLINE_TEMPLATE T &t();
00079 #else
00080         Q_INLINE_TEMPLATE T &t()
00081         { return *reinterpret_cast<T*>(QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic
00082                                        ? v : this); }
00083 #endif
00084     };
00085 
00086     union { QListData p; QListData::Data *d; };
00087 
00088 public:
00089     inline QList() : d(&QListData::shared_null) { d->ref.ref(); }
00090     inline QList(const QList<T> &l) : d(l.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
00091     ~QList();
00092     QList<T> &operator=(const QList<T> &l);
00093     bool operator==(const QList<T> &l) const;
00094     inline bool operator!=(const QList<T> &l) const { return !(*this == l); }
00095 
00096     inline int size() const { return p.size(); }
00097 
00098     inline void detach() { if (d->ref != 1) detach_helper(); }
00099     inline bool isDetached() const { return d->ref == 1; }
00100     inline void setSharable(bool sharable) { if (!sharable) detach(); d->sharable = sharable; }
00101 
00102     inline bool isEmpty() const { return p.isEmpty(); }
00103 
00104     void clear();
00105 
00106     const T &at(int i) const;
00107     const T &operator[](int i) const;
00108     T &operator[](int i);
00109 
00110     void append(const T &t);
00111     void prepend(const T &t);
00112     void insert(int i, const T &t);
00113     void replace(int i, const T &t);
00114     void removeAt(int i);
00115     int removeAll(const T &t);
00116     T takeAt(int i);
00117     T takeFirst();
00118     T takeLast();
00119     void move(int from, int to);
00120     void swap(int i, int j);
00121     int indexOf(const T &t, int from = 0) const;
00122     int lastIndexOf(const T &t, int from = -1) const;
00123     QBool contains(const T &t) const;
00124     int count(const T &t) const;
00125 
00126     class const_iterator;
00127 
00128     class iterator {
00129     public:
00130         Node *i;
00131         typedef std::random_access_iterator_tag  iterator_category;
00132         typedef ptrdiff_t  difference_type;
00133         typedef T value_type;
00134         typedef T *pointer;
00135         typedef T &reference;
00136 
00137         inline iterator() : i(0) {}
00138         inline iterator(Node *n) : i(n) {}
00139         inline iterator(const iterator &o): i(o.i){}
00140         inline T &operator*() const { return i->t(); }
00141         inline T *operator->() const { return &i->t(); }
00142         inline T &operator[](int j) const { return i[j].t(); }
00143         inline bool operator==(const iterator &o) const { return i == o.i; }
00144         inline bool operator!=(const iterator &o) const { return i != o.i; }
00145         inline bool operator<(const iterator& other) const { return i < other.i; }
00146         inline bool operator<=(const iterator& other) const { return i <= other.i; }
00147         inline bool operator>(const iterator& other) const { return i > other.i; }
00148         inline bool operator>=(const iterator& other) const { return i >= other.i; }
00149 #ifndef QT_STRICT_ITERATORS
00150         inline bool operator==(const const_iterator &o) const
00151             { return i == reinterpret_cast<const iterator &>(o).i; }
00152         inline bool operator!=(const const_iterator &o) const
00153             { return i != reinterpret_cast<const iterator &>(o).i; }
00154         inline bool operator<(const const_iterator& other) const
00155             { return i < reinterpret_cast<const iterator &>(other).i; }
00156         inline bool operator<=(const const_iterator& other) const
00157             { return i <= reinterpret_cast<const iterator &>(other).i; }
00158         inline bool operator>(const const_iterator& other) const
00159             { return i > reinterpret_cast<const iterator &>(other).i; }
00160         inline bool operator>=(const const_iterator& other) const
00161             { return i >= reinterpret_cast<const iterator &>(other).i; }
00162 #endif
00163         inline iterator &operator++() { ++i; return *this; }
00164         inline iterator operator++(int) { Node *n = i; ++i; return n; }
00165         inline iterator &operator--() { i--; return *this; }
00166         inline iterator operator--(int) { Node *n = i; i--; return n; }
00167         inline iterator &operator+=(int j) { i+=j; return *this; }
00168         inline iterator &operator-=(int j) { i-=j; return *this; }
00169         inline iterator operator+(int j) const { return iterator(i+j); }
00170         inline iterator operator-(int j) const { return iterator(i-j); }
00171         inline int operator-(iterator j) const { return i - j.i; }
00172     };
00173     friend class iterator;
00174 
00175     class const_iterator {
00176     public:
00177         Node *i;
00178         typedef std::random_access_iterator_tag  iterator_category;
00179         typedef ptrdiff_t difference_type;
00180         typedef T value_type;
00181         typedef const T *pointer;
00182         typedef const T &reference;
00183 
00184         inline const_iterator() : i(0) {}
00185         inline const_iterator(Node *n) : i(n) {}
00186         inline const_iterator(const const_iterator &o): i(o.i) {}
00187 #ifdef QT_STRICT_ITERATORS
00188         inline explicit const_iterator(const iterator &o): i(o.i) {}
00189 #else
00190         inline const_iterator(const iterator &o): i(o.i) {}
00191 #endif
00192         inline const T &operator*() const { return i->t(); }
00193         inline const T *operator->() const { return &i->t(); }
00194         inline const T &operator[](int j) const { return i[j].t(); }
00195         inline bool operator==(const const_iterator &o) const { return i == o.i; }
00196         inline bool operator!=(const const_iterator &o) const { return i != o.i; }
00197         inline bool operator<(const const_iterator& other) const { return i < other.i; }
00198         inline bool operator<=(const const_iterator& other) const { return i <= other.i; }
00199         inline bool operator>(const const_iterator& other) const { return i > other.i; }
00200         inline bool operator>=(const const_iterator& other) const { return i >= other.i; }
00201         inline const_iterator &operator++() { ++i; return *this; }
00202         inline const_iterator operator++(int) { Node *n = i; ++i; return n; }
00203         inline const_iterator &operator--() { i--; return *this; }
00204         inline const_iterator operator--(int) { Node *n = i; i--; return n; }
00205         inline const_iterator &operator+=(int j) { i+=j; return *this; }
00206         inline const_iterator &operator-=(int j) { i-=j; return *this; }
00207         inline const_iterator operator+(int j) const { return const_iterator(i+j); }
00208         inline const_iterator operator-(int j) const { return const_iterator(i-j); }
00209         inline int operator-(const_iterator j) const { return i - j.i; }
00210     };
00211     friend class const_iterator;
00212 
00213     // stl style
00214     inline iterator begin() { detach(); return reinterpret_cast<Node *>(p.begin()); }
00215     inline const_iterator begin() const { return reinterpret_cast<Node *>(p.begin()); }
00216     inline const_iterator constBegin() const { return reinterpret_cast<Node *>(p.begin()); }
00217     inline iterator end() { detach(); return reinterpret_cast<Node *>(p.end()); }
00218     inline const_iterator end() const { return reinterpret_cast<Node *>(p.end()); }
00219     inline const_iterator constEnd() const { return reinterpret_cast<Node *>(p.end()); }
00220     iterator insert(iterator before, const T &t);
00221     iterator erase(iterator pos);
00222     iterator erase(iterator first, iterator last);
00223 
00224     // more Qt
00225     typedef iterator Iterator;
00226     typedef const_iterator ConstIterator;
00227     inline int count() const { return p.size(); }
00228     inline T& first() { Q_ASSERT(!isEmpty()); return *begin(); }
00229     inline const T& first() const { Q_ASSERT(!isEmpty()); return *begin(); }
00230     T& last() { Q_ASSERT(!isEmpty()); return *(--end()); }
00231     const T& last() const { Q_ASSERT(!isEmpty()); return *(--end()); }
00232     inline void removeFirst() { Q_ASSERT(!isEmpty()); erase(begin()); }
00233     inline void removeLast() { Q_ASSERT(!isEmpty()); erase(--end()); }
00234     QList<T> mid(int pos, int length = -1) const;
00235 
00236     T value(int i) const;
00237     T value(int i, const T &defaultValue) const;
00238 
00239     // stl compatibility
00240     inline void push_back(const T &t) { append(t); }
00241     inline void push_front(const T &t) { prepend(t); }
00242     inline T& front() { return first(); }
00243     inline const T& front() const { return first(); }
00244     inline T& back() { return last(); }
00245     inline const T& back() const { return last(); }
00246     inline void pop_front() { removeFirst(); }
00247     inline void pop_back() { removeLast(); }
00248     inline bool empty() const { return isEmpty(); }
00249     typedef int size_type;
00250     typedef T value_type;
00251     typedef value_type *pointer;
00252     typedef const value_type *const_pointer;
00253     typedef value_type &reference;
00254     typedef const value_type &const_reference;
00255     typedef ptrdiff_t difference_type;
00256 
00257 #ifdef QT3_SUPPORT
00258     inline QT3_SUPPORT iterator remove(iterator pos) { return erase(pos); }
00259     inline QT3_SUPPORT int remove(const T &t) { return removeAll(t); }
00260     inline QT3_SUPPORT int findIndex(const T& t) const { return indexOf(t); }
00261     inline QT3_SUPPORT iterator find(const T& t)
00262     { int i = indexOf(t); return (i == -1 ? end() : (begin()+i)); }
00263     inline QT3_SUPPORT const_iterator find (const T& t) const
00264     { int i = indexOf(t); return (i == -1 ? end() : (begin()+i)); }
00265     inline QT3_SUPPORT iterator find(iterator from, const T& t)
00266     { int i = indexOf(t, from - begin()); return i == -1 ? end() : begin()+i; }
00267     inline QT3_SUPPORT const_iterator find(const_iterator from, const T& t) const
00268     { int i = indexOf(t, from - begin()); return i == -1 ? end() : begin()+i; }
00269 #endif
00270 
00271     // comfort
00272     QList<T> &operator+=(const QList<T> &l);
00273     inline QList<T> operator+(const QList<T> &l) const
00274     { QList n = *this; n += l; return n; }
00275     inline QList<T> &operator+=(const T &t)
00276     { append(t); return *this; }
00277     inline QList<T> &operator<< (const T &t)
00278     { append(t); return *this; }
00279     inline QList<T> &operator<<(const QList<T> &l)
00280     { *this += l; return *this; }
00281 
00282     QVector<T> toVector() const;
00283     QSet<T> toSet() const;
00284 
00285     static QList<T> fromVector(const QVector<T> &vector);
00286     static QList<T> fromSet(const QSet<T> &set);
00287 
00288 #ifndef QT_NO_STL
00289     static inline QList<T> fromStdList(const std::list<T> &list)
00290     { QList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
00291     inline std::list<T> toStdList() const
00292     { std::list<T> tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
00293 #endif
00294 
00295 private:
00296     void detach_helper();
00297     void free(QListData::Data *d);
00298 
00299     void node_construct(Node *n, const T &t);
00300     void node_destruct(Node *n);
00301     void node_copy(Node *from, Node *to, Node *src);
00302     void node_destruct(Node *from, Node *to);
00303 };
00304 
00305 #if defined(Q_CC_BOR)
00306 template <typename T>
00307 Q_INLINE_TEMPLATE T &QList<T>::Node::t()
00308 { return QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic ? *(T*)v:*(T*)this; }
00309 #endif
00310 
00311 template <typename T>
00312 Q_INLINE_TEMPLATE void QList<T>::node_construct(Node *n, const T &t)
00313 {
00314     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) n->v = new T(t);
00315     else if (QTypeInfo<T>::isComplex) new (n) T(t);
00316     else *reinterpret_cast<T*>(n) = t;
00317 }
00318 
00319 template <typename T>
00320 Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n)
00321 {
00322     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v);
00323     else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T();
00324 }
00325 
00326 template <typename T>
00327 Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src)
00328 {
00329     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic)
00330         while(from != to)
00331             (from++)->v = new T(*reinterpret_cast<T*>((src++)->v));
00332     else if (QTypeInfo<T>::isComplex)
00333         while(from != to)
00334             new (from++) T(*reinterpret_cast<T*>(src++));
00335 }
00336 
00337 template <typename T>
00338 Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to)
00339 {
00340     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic)
00341         while(from != to) --to, delete reinterpret_cast<T*>(to->v);
00342     else if (QTypeInfo<T>::isComplex)
00343         while (from != to) --to, reinterpret_cast<T*>(to)->~T();
00344 }
00345 
00346 template <typename T>
00347 Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l)
00348 {
00349     if (d != l.d) {
00350         QListData::Data *x = l.d;
00351         x->ref.ref();
00352         x = qAtomicSetPtr(&d, x);
00353         if (!x->ref.deref())
00354             free(x);
00355         if (!d->sharable)
00356             detach_helper();
00357     }
00358     return *this;
00359 }
00360 template <typename T>
00361 inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t)
00362 { Node *n = reinterpret_cast<Node *>(p.insert(before.i-reinterpret_cast<Node *>(p.begin())));
00363  node_construct(n,t); return n; }
00364 template <typename T>
00365 inline typename QList<T>::iterator QList<T>::erase(iterator it)
00366 { node_destruct(it.i);
00367  return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); }
00368 template <typename T>
00369 inline const T &QList<T>::at(int i) const
00370 { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
00371  return reinterpret_cast<Node *>(p.at(i))->t(); }
00372 template <typename T>
00373 inline const T &QList<T>::operator[](int i) const
00374 { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
00375  return reinterpret_cast<Node *>(p.at(i))->t(); }
00376 template <typename T>
00377 inline T &QList<T>::operator[](int i)
00378 { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
00379   detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }
00380 template <typename T>
00381 inline void QList<T>::removeAt(int i)
00382 { if(i >= 0 && i < p.size()) { detach();
00383  node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); } }
00384 template <typename T>
00385 inline T QList<T>::takeAt(int i)
00386 { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range");
00387  detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = n->t(); node_destruct(n);
00388  p.remove(i); return t; }
00389 template <typename T>
00390 inline T QList<T>::takeFirst()
00391 { T t = first(); removeFirst(); return t; }
00392 template <typename T>
00393 inline T QList<T>::takeLast()
00394 { T t = last(); removeLast(); return t; }
00395 
00396 template <typename T>
00397 Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t)
00398 {
00399     detach();
00400     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
00401         node_construct(reinterpret_cast<Node *>(p.append()), t);
00402     } else {
00403         const T cpy(t);
00404         node_construct(reinterpret_cast<Node *>(p.append()), cpy);
00405     }
00406 }
00407 
00408 template <typename T>
00409 inline void QList<T>::prepend(const T &t)
00410 {
00411     detach();
00412     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
00413         node_construct(reinterpret_cast<Node *>(p.prepend()), t);
00414     } else {
00415         const T cpy(t);
00416         node_construct(reinterpret_cast<Node *>(p.prepend()), cpy);
00417     }
00418 }
00419 
00420 template <typename T>
00421 inline void QList<T>::insert(int i, const T &t)
00422 {
00423     detach();
00424     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
00425         node_construct(reinterpret_cast<Node *>(p.insert(i)), t);
00426     } else {
00427         const T cpy(t);
00428         node_construct(reinterpret_cast<Node *>(p.insert(i)), cpy);
00429     }
00430 }
00431 
00432 template <typename T>
00433 inline void QList<T>::replace(int i, const T &t)
00434 {
00435     Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range");
00436     detach();
00437     if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
00438         reinterpret_cast<Node *>(p.at(i))->t() = t;
00439     } else {
00440         const T cpy(t);
00441         reinterpret_cast<Node *>(p.at(i))->t() = cpy;
00442     }
00443 }
00444 
00445 template <typename T>
00446 inline void QList<T>::swap(int i, int j)
00447 {
00448     Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(),
00449                 "QList<T>::swap", "index out of range");
00450     detach();
00451     void *t = d->array[d->begin + i];
00452     d->array[d->begin + i] = d->array[d->begin + j];
00453     d->array[d->begin + j] = t;
00454 }
00455 
00456 template <typename T>
00457 inline void QList<T>::move(int from, int to)
00458 {
00459     Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(),
00460                "QList<T>::move", "index out of range");
00461     detach();
00462     p.move(from, to);
00463 }
00464 
00465 template<typename T>
00466 Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int length) const
00467 {
00468     if (length < 0)
00469         length = size() - pos;
00470     if (pos == 0 && length == size())
00471         return *this;
00472     QList<T> cpy;
00473     if (pos + length > size())
00474         length = size() - pos;
00475     for (int i = pos; i < pos + length; ++i)
00476         cpy += at(i);
00477     return cpy;
00478 }
00479 
00480 template<typename T>
00481 Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const
00482 {
00483     if (i < 0 || i >= p.size()) {
00484         return T();
00485     }
00486     return reinterpret_cast<Node *>(p.at(i))->t();
00487 }
00488 template<typename T>
00489 Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const
00490 {
00491     return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t());
00492 }
00493 
00494 template <typename T>
00495 Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper()
00496 {
00497     Node *n = reinterpret_cast<Node *>(p.begin());
00498     QListData::Data *x = p.detach();
00499     if (x)
00500         free(x);
00501     node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n);
00502 }
00503 
00504 template <typename T>
00505 Q_OUTOFLINE_TEMPLATE QList<T>::~QList()
00506 {
00507     if (!d)
00508         return;
00509     QListData::Data *x = &QListData::shared_null;
00510     x = qAtomicSetPtr(&d, x);
00511     if (!x->ref.deref())
00512         free(x);
00513 }
00514 
00515 template <typename T>
00516 Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const
00517 {
00518     if (p.size() != l.p.size())
00519         return false;
00520     if (d == l.d)
00521         return true;
00522     Node *i = reinterpret_cast<Node *>(p.end());
00523     Node *b = reinterpret_cast<Node *>(p.begin());
00524     Node *li = reinterpret_cast<Node *>(l.p.end());
00525     while (i != b) {
00526         --i; --li;
00527         if (!(i->t() == li->t()))
00528             return false;
00529     }
00530     return true;
00531 }
00532 
00533 
00534 template <typename T>
00535 Q_OUTOFLINE_TEMPLATE void QList<T>::free(QListData::Data *data)
00536 {
00537     node_destruct(reinterpret_cast<Node *>(data->array + data->begin),
00538                   reinterpret_cast<Node *>(data->array + data->end));
00539     if (data->ref == 0)
00540         qFree(data);
00541 }
00542 
00543 
00544 template <typename T>
00545 Q_OUTOFLINE_TEMPLATE void QList<T>::clear()
00546 {
00547     *this = QList<T>();
00548 }
00549 
00550 template <typename T>
00551 Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t)
00552 {
00553     detach();
00554     const T t = _t;
00555     int removedCount=0, i=0;
00556     Node *n;
00557     while (i < p.size())
00558         if ((n = reinterpret_cast<Node *>(p.at(i)))->t() == t) {
00559             node_destruct(n);
00560             p.remove(i);
00561             ++removedCount;
00562         } else {
00563             ++i;
00564         }
00565     return removedCount;
00566 }
00567 
00568 template <typename T>
00569 Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst,
00570                                                                  typename QList<T>::iterator alast)
00571 {
00572     for (Node *n = afirst.i; n < alast.i; ++n)
00573         node_destruct(n);
00574     int idx = afirst - begin();
00575     p.remove(idx, alast - afirst);
00576     return begin() + idx;
00577 }
00578 
00579 template <typename T>
00580 Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l)
00581 {
00582     detach();
00583     Node *n = reinterpret_cast<Node *>(p.append(l.p));
00584     node_copy(n, reinterpret_cast<Node *>(p.end()), reinterpret_cast<Node *>(l.p.begin()));
00585     return *this;
00586 }
00587 
00588 template <typename T>
00589 Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const
00590 {
00591     if (from < 0)
00592         from = qMax(from + p.size(), 0);
00593     if (from < p.size()) {
00594         Node *n = reinterpret_cast<Node *>(p.at(from -1));
00595         Node *e = reinterpret_cast<Node *>(p.end());
00596         while (++n != e)
00597             if (n->t() == t)
00598                 return n - reinterpret_cast<Node *>(p.begin());
00599     }
00600     return -1;
00601 }
00602 
00603 template <typename T>
00604 Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const
00605 {
00606     if (from < 0)
00607         from += p.size();
00608     else if (from >= p.size())
00609         from = p.size()-1;
00610     if (from >= 0) {
00611         Node *b = reinterpret_cast<Node *>(p.begin());
00612         Node *n = reinterpret_cast<Node *>(p.at(from + 1));
00613         while (n-- != b) {
00614             if (n->t() == t)
00615                 return n - b;
00616         }
00617     }
00618     return -1;
00619 }
00620 
00621 template <typename T>
00622 Q_OUTOFLINE_TEMPLATE QBool QList<T>::contains(const T &t) const
00623 {
00624     Node *b = reinterpret_cast<Node *>(p.begin());
00625     Node *i = reinterpret_cast<Node *>(p.end());
00626     while (i-- != b)
00627         if (i->t() == t)
00628             return QBool(true);
00629     return QBool(false);
00630 }
00631 
00632 template <typename T>
00633 Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const
00634 {
00635     int c = 0;
00636     Node *b = reinterpret_cast<Node *>(p.begin());
00637     Node *i = reinterpret_cast<Node *>(p.end());
00638     while (i-- != b)
00639         if (i->t() == t)
00640             ++c;
00641     return c;
00642 }
00643 
00644 Q_DECLARE_SEQUENTIAL_ITERATOR(List)
00645 Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)
00646 
00647 QT_END_HEADER
00648 
00649 #endif // QLIST_H

Generated on Thu Mar 15 11:54:04 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1