src/corelib/tools/qvarlengtharray.h
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   105     }
   105     }
   106     inline const T &operator[](int idx) const {
   106     inline const T &operator[](int idx) const {
   107         Q_ASSERT(idx >= 0 && idx < s);
   107         Q_ASSERT(idx >= 0 && idx < s);
   108         return ptr[idx];
   108         return ptr[idx];
   109     }
   109     }
       
   110     inline const T &at(int idx) const { return operator[](idx); }
       
   111 
       
   112     T value(int i) const;
       
   113     T value(int i, const T &defaultValue) const;
   110 
   114 
   111     inline void append(const T &t) {
   115     inline void append(const T &t) {
   112         if (s == a)   // i.e. s != 0
   116         if (s == a)   // i.e. s != 0
   113             realloc(s, s<<1);
   117             realloc(s, s<<1);
   114         const int idx = s++;
   118         const int idx = s++;
   126 
   130 
   127 private:
   131 private:
   128     friend class QPodList<T, Prealloc>;
   132     friend class QPodList<T, Prealloc>;
   129     void realloc(int size, int alloc);
   133     void realloc(int size, int alloc);
   130 
   134 
   131     int a;      // capacity
   135     int a;
   132     int s;      // size
   136     int s;
   133     T *ptr;     // data
   137     T *ptr;
   134     union {
   138     union {
   135         // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
   139         // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
   136         char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
   140         char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
   137         qint64 q_for_alignment_1;
   141         qint64 q_for_alignment_1;
   138         double q_for_alignment_2;
   142         double q_for_alignment_2;
   191 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
   195 Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
   192 {
   196 {
   193     Q_ASSERT(aalloc >= asize);
   197     Q_ASSERT(aalloc >= asize);
   194     T *oldPtr = ptr;
   198     T *oldPtr = ptr;
   195     int osize = s;
   199     int osize = s;
   196 
   200     // s = asize;
   197     const int copySize = qMin(asize, osize);
   201 
   198     if (aalloc != a) {
   202     if (aalloc != a) {
   199         ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
   203         ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
   200         Q_CHECK_PTR(ptr);
   204         Q_CHECK_PTR(ptr);
   201         if (ptr) {
   205         if (ptr) {
   202             s = 0;
   206             s = 0;
   203             a = aalloc;
   207             a = aalloc;
   204 
   208 
   205             if (QTypeInfo<T>::isStatic) {
   209             if (QTypeInfo<T>::isStatic) {
   206                 QT_TRY {
   210                 QT_TRY {
   207                     // copy all the old elements
   211                     // copy all the old elements
       
   212                     const int copySize = qMin(asize, osize);
   208                     while (s < copySize) {
   213                     while (s < copySize) {
   209                         new (ptr+s) T(*(oldPtr+s));
   214                         new (ptr+s) T(*(oldPtr+s));
   210                         (oldPtr+s)->~T();
   215                         (oldPtr+s)->~T();
   211                         s++;
   216                         s++;
   212                     }
   217                     }
   218                     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   223                     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   219                         qFree(oldPtr);
   224                         qFree(oldPtr);
   220                     QT_RETHROW;
   225                     QT_RETHROW;
   221                 }
   226                 }
   222             } else {
   227             } else {
   223                 qMemCopy(ptr, oldPtr, copySize * sizeof(T));
   228                 qMemCopy(ptr, oldPtr, qMin(asize, osize) * sizeof(T));
   224             }
   229             }
   225         } else {
   230         } else {
   226             ptr = oldPtr;
   231             ptr = oldPtr;
   227             return;
   232             return;
   228         }
   233         }
   229     }
   234     }
   230     s = copySize;
   235 
   231 
   236     if (QTypeInfo<T>::isComplex) {
   232     if (QTypeInfo<T>::isComplex) {
       
   233         // destroy remaining old objects
       
   234         while (osize > asize)
   237         while (osize > asize)
   235             (oldPtr+(--osize))->~T();
   238             (oldPtr+(--osize))->~T();
       
   239         if (!QTypeInfo<T>::isStatic)
       
   240             s = osize;
   236     }
   241     }
   237 
   242 
   238     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   243     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
   239         qFree(oldPtr);
   244         qFree(oldPtr);
   240 
   245 
   245     } else {
   250     } else {
   246         s = asize;
   251         s = asize;
   247     }
   252     }
   248 }
   253 }
   249 
   254 
       
   255 template <class T, int Prealloc>
       
   256 Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
       
   257 {
       
   258     if (i < 0 || i >= size()) {
       
   259         return T();
       
   260     }
       
   261     return at(i);
       
   262 }
       
   263 template <class T, int Prealloc>
       
   264 Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
       
   265 {
       
   266     return (i < 0 || i >= size()) ? defaultValue : at(i);
       
   267 }
       
   268 
       
   269 
   250 QT_END_NAMESPACE
   270 QT_END_NAMESPACE
   251 
   271 
   252 QT_END_HEADER
   272 QT_END_HEADER
   253 
   273 
   254 #endif // QVARLENGTHARRAY_H
   274 #endif // QVARLENGTHARRAY_H