⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qvector.h

📁 奇趣公司比较新的qt/emd版本
💻 H
📖 第 1 页 / 共 2 页
字号:
    } else {        qMemSet(d->array, 0, asize * sizeof(T));    }}template <typename T>QVector<T>::QVector(int asize, const T &t){    p = malloc(asize);    d->ref.init(1);    d->alloc = d->size = asize;    d->sharable = true;    d->capacity = false;    T* i = d->array + d->size;    while (i != d->array)        new (--i) T(t);}template <typename T>void QVector<T>::free(Data *x){    if (QTypeInfo<T>::isComplex) {        T* b = x->array;        T* i = b + x->size;        while (i-- != b)             i->~T();    }    qFree(x);}template <typename T>void QVector<T>::realloc(int asize, int aalloc){    T *j, *i, *b;    union { QVectorData *p; Data *d; } x;    x.d = d;    if (QTypeInfo<T>::isComplex && aalloc == d->alloc && d->ref == 1) {        // pure resize        i = d->array + d->size;        j = d->array + asize;        if (i > j) {            while (i-- != j)                i->~T();        } else {            while (j-- != i)                new (j) T;        }        d->size = asize;        return;    }    if (aalloc != d->alloc || d->ref != 1) {        // (re)allocate memory        if (QTypeInfo<T>::isStatic) {            x.p = malloc(aalloc);        } else if (d->ref != 1) {            x.p = QVectorData::malloc(sizeof(Data), aalloc, sizeof(T), p);        } else {            if (QTypeInfo<T>::isComplex) {                // call the destructor on all objects that need to be                // destroyed when shrinking                if (asize < d->size) {                    j = d->array + asize;                    i = d->array + d->size;                    while (i-- != j)                        i->~T();                    i = d->array + asize;                }            }            x.p = p = static_cast<QVectorData *>(qRealloc(p, sizeof(Data) + (aalloc - 1) * sizeof(T)));        }        x.d->ref.init(1);        x.d->sharable = true;        x.d->capacity = d->capacity;    }    if (QTypeInfo<T>::isComplex) {        if (asize < d->size) {            j = d->array + asize;            i = x.d->array + asize;        } else {            // construct all new objects when growing            i = x.d->array + asize;            j = x.d->array + d->size;            while (i != j)                new (--i) T;            j = d->array + d->size;        }        if (i != j) {            // copy objects from the old array into the new array            b = x.d->array;            while (i != b)                new (--i) T(*--j);        }    } else if (asize > d->size) {        // initialize newly allocated memory to 0        qMemSet(x.d->array + d->size, 0, (asize - d->size) * sizeof(T));    }    x.d->size = asize;    x.d->alloc = aalloc;    if (d != x.d) {        x.d = qAtomicSetPtr(&d, x.d);        if (!x.d->ref.deref())            free(x.d);    }}template<typename T>Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i) const{    if (i < 0 || i >= p->size) {        return T();    }    return d->array[i];}template<typename T>Q_OUTOFLINE_TEMPLATE T QVector<T>::value(int i, const T &defaultValue) const{    return ((i < 0 || i >= p->size) ? defaultValue : d->array[i]);}template <typename T>void QVector<T>::append(const T &t){    if (d->ref != 1 || d->size + 1 > d->alloc) {        const T copy(t);        realloc(d->size, QVectorData::grow(sizeof(Data), d->size + 1, sizeof(T),                                           QTypeInfo<T>::isStatic));        if (QTypeInfo<T>::isComplex)            new (d->array + d->size) T(copy);        else            d->array[d->size] = copy;    } else {        if (QTypeInfo<T>::isComplex)            new (d->array + d->size) T(t);        else            d->array[d->size] = t;    }    ++d->size;}template <typename T>Q_TYPENAME QVector<T>::iterator QVector<T>::insert(iterator before, size_type n, const T &t){    int offset = before - d->array;    if (n != 0) {        const T copy(t);        if (d->ref != 1 || d->size + n > d->alloc)            realloc(d->size, QVectorData::grow(sizeof(Data), d->size + n, sizeof(T),                                               QTypeInfo<T>::isStatic));        if (QTypeInfo<T>::isStatic) {            T *b = d->array + d->size;            T *i = d->array + d->size + n;            while (i != b)                new (--i) T;            i = d->array + d->size;            T *j = i + n;            b = d->array + offset;            while (i != b)                *--j = *--i;            i = b+n;            while (i != b)                *--i = copy;        } else {            T *b = d->array + offset;            T *i = b + n;            memmove(i, b, (d->size - offset) * sizeof(T));            while (i != b)                new (--i) T(copy);        }        d->size += n;    }    return d->array + offset;}template <typename T>Q_TYPENAME QVector<T>::iterator QVector<T>::erase(iterator abegin, iterator aend){    int f = abegin - d->array;    int l = aend - d->array;    int n = l - f;    detach();    if (QTypeInfo<T>::isComplex) {        qCopy(d->array+l, d->array+d->size, d->array+f);        T *i = d->array+d->size;        T* b = d->array+d->size-n;        while (i != b) {            --i;            i->~T();        }    } else {        memmove(d->array + f, d->array + l, (d->size-l)*sizeof(T));    }    d->size -= n;    return d->array + f;}template <typename T>bool QVector<T>::operator==(const QVector<T> &v) const{    if (d->size != v.d->size)        return false;    if (d == v.d)        return true;    T* b = d->array;    T* i = b + d->size;    T* j = v.d->array + d->size;    while (i != b)        if (!(*--i == *--j))            return false;    return true;}template <typename T>QVector<T> &QVector<T>::fill(const T &from, int asize){    const T copy(from);    resize(asize < 0 ? d->size : asize);    if (d->size) {        T *i = d->array + d->size;        T *b = d->array;        while (i != b)            *--i = copy;    }    return *this;}template <typename T>QVector<T> &QVector<T>::operator+=(const QVector &l){    int newSize = d->size + l.d->size;    realloc(d->size, newSize);    T *w = d->array + newSize;    T *i = l.d->array + l.d->size;    T *b = l.d->array;    while (i != b) {        if (QTypeInfo<T>::isComplex)            new (--w) T(*--i);        else            *--w = *--i;    }    d->size = newSize;    return *this;}template <typename T>int QVector<T>::indexOf(const T &t, int from) const{    if (from < 0)        from = qMax(from + d->size, 0);    if (from < d->size) {        T* n = d->array + from - 1;        T* e = d->array + d->size;        while (++n != e)            if (*n == t)                return n - d->array;    }    return -1;}template <typename T>int QVector<T>::lastIndexOf(const T &t, int from) const{    if (from < 0)        from += d->size;    else if (from >= d->size)        from = d->size-1;    if (from >= 0) {        T* b = d->array;        T* n = d->array + from + 1;        while (n != b) {            if (*--n == t)                return n - b;        }    }    return -1;}template <typename T>bool QVector<T>::contains(const T &t) const{    T* b = d->array;    T* i = d->array + d->size;    while (i != b)        if (*--i == t)            return true;    return false;}template <typename T>int QVector<T>::count(const T &t) const{    int c = 0;    T* b = d->array;    T* i = d->array + d->size;    while (i != b)        if (*--i == t)            ++c;    return c;}template <typename T>Q_OUTOFLINE_TEMPLATE QVector<T> QVector<T>::mid(int pos, int length) const{    if (length < 0)        length = size() - pos;    if (pos == 0 && length == size())        return *this;    QVector<T> copy;    if (pos + length > size())        length = size() - pos;    for (int i = pos; i < pos + length; ++i)        copy += at(i);    return copy;}template <typename T>Q_OUTOFLINE_TEMPLATE QList<T> QVector<T>::toList() const{    QList<T> result;    for (int i = 0; i < size(); ++i)        result.append(at(i));    return result;}template <typename T>Q_OUTOFLINE_TEMPLATE QVector<T> QList<T>::toVector() const{    QVector<T> result(size());    for (int i = 0; i < size(); ++i)        result[i] = at(i);    return result;}template <typename T>QVector<T> QVector<T>::fromList(const QList<T> &list){    return list.toVector();}template <typename T>QList<T> QList<T>::fromVector(const QVector<T> &vector){    return vector.toList();}Q_DECLARE_SEQUENTIAL_ITERATOR(Vector)Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(Vector)/*   ### Qt 5:   ### This needs to be removed for next releases of Qt. It is a workaround for vc++ because   ### Qt exports QPolygon and QPolygonF that inherit QVector<QPoint> and   ### QVector<QPointF> respectively.*/#ifdef Q_CC_MSVC#include <QtCore/QPointF>#include <QtCore/QPoint>#if defined(QT_BUILD_CORE_LIB)#define Q_TEMPLATE_EXTERN#else#define Q_TEMPLATE_EXTERN extern#endif# pragma warning(push)          /* MSVC 6.0 doesn't care about the disabling in qglobal.h (why?), so do it here */# pragma warning(disable: 4231) /* nonstandard extension used : 'extern' before template explicit instantiation */Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPointF>;Q_TEMPLATE_EXTERN template class Q_CORE_EXPORT QVector<QPoint>;# pragma warning(pop)#endifQT_END_HEADER#endif // QVECTOR_H

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -