📄 qlist.h
字号:
Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *n){ if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) delete reinterpret_cast<T*>(n->v); else if (QTypeInfo<T>::isComplex) reinterpret_cast<T*>(n)->~T();}template <typename T>Q_INLINE_TEMPLATE void QList<T>::node_copy(Node *from, Node *to, Node *src){ if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) while(from != to) (from++)->v = new T(*reinterpret_cast<T*>((src++)->v)); else if (QTypeInfo<T>::isComplex) while(from != to) new (from++) T(*reinterpret_cast<T*>(src++));}template <typename T>Q_INLINE_TEMPLATE void QList<T>::node_destruct(Node *from, Node *to){ if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) while(from != to) --to, delete reinterpret_cast<T*>(to->v); else if (QTypeInfo<T>::isComplex) while (from != to) --to, reinterpret_cast<T*>(to)->~T();}template <typename T>Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l){ if (d != l.d) { QListData::Data *x = l.d; x->ref.ref(); x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x); if (!d->sharable) detach_helper(); } return *this;}template <typename T>inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t){ Node *n = reinterpret_cast<Node *>(p.insert(before.i-reinterpret_cast<Node *>(p.begin()))); node_construct(n,t); return n; }template <typename T>inline typename QList<T>::iterator QList<T>::erase(iterator it){ node_destruct(it.i); return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); }template <typename T>inline const T &QList<T>::at(int i) const{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range"); return reinterpret_cast<Node *>(p.at(i))->t(); }template <typename T>inline const T &QList<T>::operator[](int i) const{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); return reinterpret_cast<Node *>(p.at(i))->t(); }template <typename T>inline T &QList<T>::operator[](int i){ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range"); detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }template <typename T>inline void QList<T>::removeAt(int i){ if(i >= 0 && i < p.size()) { detach(); node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); } }template <typename T>inline T QList<T>::takeAt(int i){ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range"); detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = n->t(); node_destruct(n); p.remove(i); return t; }template <typename T>inline T QList<T>::takeFirst(){ T t = first(); removeFirst(); return t; }template <typename T>inline T QList<T>::takeLast(){ T t = last(); removeLast(); return t; }template <typename T>Q_OUTOFLINE_TEMPLATE void QList<T>::append(const T &t){ detach(); if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { node_construct(reinterpret_cast<Node *>(p.append()), t); } else { const T cpy(t); node_construct(reinterpret_cast<Node *>(p.append()), cpy); }}template <typename T>inline void QList<T>::prepend(const T &t){ detach(); if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { node_construct(reinterpret_cast<Node *>(p.prepend()), t); } else { const T cpy(t); node_construct(reinterpret_cast<Node *>(p.prepend()), cpy); }}template <typename T>inline void QList<T>::insert(int i, const T &t){ detach(); if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { node_construct(reinterpret_cast<Node *>(p.insert(i)), t); } else { const T cpy(t); node_construct(reinterpret_cast<Node *>(p.insert(i)), cpy); }}template <typename T>inline void QList<T>::replace(int i, const T &t){ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range"); detach(); if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) { reinterpret_cast<Node *>(p.at(i))->t() = t; } else { const T cpy(t); reinterpret_cast<Node *>(p.at(i))->t() = cpy; }}template <typename T>inline void QList<T>::swap(int i, int j){ Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(), "QList<T>::swap", "index out of range"); detach(); void *t = d->array[d->begin + i]; d->array[d->begin + i] = d->array[d->begin + j]; d->array[d->begin + j] = t;}template <typename T>inline void QList<T>::move(int from, int to){ Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(), "QList<T>::move", "index out of range"); detach(); p.move(from, to);}template<typename T>Q_OUTOFLINE_TEMPLATE QList<T> QList<T>::mid(int pos, int length) const{ if (length < 0) length = size() - pos; if (pos == 0 && length == size()) return *this; QList<T> cpy; if (pos + length > size()) length = size() - pos; for (int i = pos; i < pos + length; ++i) cpy += at(i); return cpy;}template<typename T>Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i) const{ if (i < 0 || i >= p.size()) { return T(); } return reinterpret_cast<Node *>(p.at(i))->t();}template<typename T>Q_OUTOFLINE_TEMPLATE T QList<T>::value(int i, const T& defaultValue) const{ return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t());}template <typename T>Q_OUTOFLINE_TEMPLATE void QList<T>::detach_helper(){ Node *n = reinterpret_cast<Node *>(p.begin()); QListData::Data *x = p.detach2(); node_copy(reinterpret_cast<Node *>(p.begin()), reinterpret_cast<Node *>(p.end()), n); if (!x->ref.deref()) free(x);}template <typename T>Q_OUTOFLINE_TEMPLATE QList<T>::~QList(){ if (!d) return; QListData::Data *x = &QListData::shared_null; x = qAtomicSetPtr(&d, x); if (!x->ref.deref()) free(x);}template <typename T>Q_OUTOFLINE_TEMPLATE bool QList<T>::operator==(const QList<T> &l) const{ if (p.size() != l.p.size()) return false; if (d == l.d) return true; Node *i = reinterpret_cast<Node *>(p.end()); Node *b = reinterpret_cast<Node *>(p.begin()); Node *li = reinterpret_cast<Node *>(l.p.end()); while (i != b) { --i; --li; if (!(i->t() == li->t())) return false; } return true;}template <typename T>Q_OUTOFLINE_TEMPLATE void QList<T>::free(QListData::Data *data){ node_destruct(reinterpret_cast<Node *>(data->array + data->begin), reinterpret_cast<Node *>(data->array + data->end)); if (data->ref == 0) qFree(data);}template <typename T>Q_OUTOFLINE_TEMPLATE void QList<T>::clear(){ *this = QList<T>();}template <typename T>Q_OUTOFLINE_TEMPLATE int QList<T>::removeAll(const T &_t){ detach(); const T t = _t; int removedCount=0, i=0; Node *n; while (i < p.size()) if ((n = reinterpret_cast<Node *>(p.at(i)))->t() == t) { node_destruct(n); p.remove(i); ++removedCount; } else { ++i; } return removedCount;}template <typename T>Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst, typename QList<T>::iterator alast){ for (Node *n = afirst.i; n < alast.i; ++n) node_destruct(n); int idx = afirst - begin(); p.remove(idx, alast - afirst); return begin() + idx;}template <typename T>Q_OUTOFLINE_TEMPLATE QList<T> &QList<T>::operator+=(const QList<T> &l){ detach(); Node *n = reinterpret_cast<Node *>(p.append(l.p)); node_copy(n, reinterpret_cast<Node *>(p.end()), reinterpret_cast<Node *>(l.p.begin())); return *this;}template <typename T>Q_OUTOFLINE_TEMPLATE int QList<T>::indexOf(const T &t, int from) const{ if (from < 0) from = qMax(from + p.size(), 0); if (from < p.size()) { Node *n = reinterpret_cast<Node *>(p.at(from -1)); Node *e = reinterpret_cast<Node *>(p.end()); while (++n != e) if (n->t() == t) return n - reinterpret_cast<Node *>(p.begin()); } return -1;}template <typename T>Q_OUTOFLINE_TEMPLATE int QList<T>::lastIndexOf(const T &t, int from) const{ if (from < 0) from += p.size(); else if (from >= p.size()) from = p.size()-1; if (from >= 0) { Node *b = reinterpret_cast<Node *>(p.begin()); Node *n = reinterpret_cast<Node *>(p.at(from + 1)); while (n-- != b) { if (n->t() == t) return n - b; } } return -1;}template <typename T>Q_OUTOFLINE_TEMPLATE QBool QList<T>::contains(const T &t) const{ Node *b = reinterpret_cast<Node *>(p.begin()); Node *i = reinterpret_cast<Node *>(p.end()); while (i-- != b) if (i->t() == t) return QBool(true); return QBool(false);}template <typename T>Q_OUTOFLINE_TEMPLATE int QList<T>::count(const T &t) const{ int c = 0; Node *b = reinterpret_cast<Node *>(p.begin()); Node *i = reinterpret_cast<Node *>(p.end()); while (i-- != b) if (i->t() == t) ++c; return c;}Q_DECLARE_SEQUENTIAL_ITERATOR(List)Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(List)QT_END_HEADER#endif // QLIST_H
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -