src/corelib/tools/qcontiguouscache.h
branchRCL_3
changeset 7 3f74d0d4af4c
parent 4 3b1da2848fc7
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
    95     typedef T value_type;
    95     typedef T value_type;
    96     typedef value_type* pointer;
    96     typedef value_type* pointer;
    97     typedef const value_type* const_pointer;
    97     typedef const value_type* const_pointer;
    98     typedef value_type& reference;
    98     typedef value_type& reference;
    99     typedef const value_type& const_reference;
    99     typedef const value_type& const_reference;
   100     typedef ptrdiff_t difference_type;
   100     typedef qptrdiff difference_type;
   101     typedef int size_type;
   101     typedef int size_type;
   102 
   102 
   103     explicit QContiguousCache(int capacity = 0);
   103     explicit QContiguousCache(int capacity = 0);
   104     QContiguousCache(const QContiguousCache<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
   104     QContiguousCache(const QContiguousCache<T> &v) : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
   105 
   105 
   219     union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
   219     union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
   220     x.d = malloc(asize);
   220     x.d = malloc(asize);
   221     x.d->alloc = asize;
   221     x.d->alloc = asize;
   222     x.d->count = qMin(d->count, asize);
   222     x.d->count = qMin(d->count, asize);
   223     x.d->offset = d->offset + d->count - x.d->count;
   223     x.d->offset = d->offset + d->count - x.d->count;
   224     x.d->start = x.d->offset % x.d->alloc;
   224     if(asize)
   225     T *dest = x.p->array + (x.d->start + x.d->count-1) % x.d->alloc;
   225         x.d->start = x.d->offset % x.d->alloc;
   226     T *src = p->array + (d->start + d->count-1) % d->alloc;
   226     else
       
   227         x.d->start = 0;
       
   228 
   227     int oldcount = x.d->count;
   229     int oldcount = x.d->count;
   228     while (oldcount--) {
   230     if(oldcount)
   229         if (QTypeInfo<T>::isComplex) {
   231     {
   230             new (dest) T(*src);
   232         T *dest = x.p->array + (x.d->start + x.d->count-1) % x.d->alloc;
   231         } else {
   233         T *src = p->array + (d->start + d->count-1) % d->alloc;
   232             *dest = *src;
   234         while (oldcount--) {
       
   235             if (QTypeInfo<T>::isComplex) {
       
   236                 new (dest) T(*src);
       
   237             } else {
       
   238                 *dest = *src;
       
   239             }
       
   240             if (dest == x.p->array)
       
   241                 dest = x.p->array + x.d->alloc;
       
   242             dest--;
       
   243             if (src == p->array)
       
   244                 src = p->array + d->alloc;
       
   245             src--;
   233         }
   246         }
   234         if (dest == x.p->array)
       
   235             dest = x.p->array + x.d->alloc;
       
   236         dest--;
       
   237         if (src == p->array)
       
   238             src = p->array + d->alloc;
       
   239         src--;
       
   240     }
   247     }
   241     /* free old */
   248     /* free old */
   242     free(p);
   249     free(p);
   243     d = x.d;
   250     d = x.d;
   244 }
   251 }