QContiguousCache< T > Class Template Reference

QContiguousCache< T > Class Template Reference

#include <qcontiguouscache.h>

Class Description

template<typename T>
class QContiguousCache< T >

Definition at line 90 of file qcontiguouscache.h.

Public Types

typedef T value_type
 
typedef value_typepointer
 
typedef const value_typeconst_pointer
 
typedef value_typereference
 
typedef const value_typeconst_reference
 
typedef qptrdiff difference_type
 
typedef int size_type
 

Public Member Functions

 QContiguousCache (int capacity=0)
 
 QContiguousCache (const QContiguousCache< T > &v)
 
 ~QContiguousCache ()
 
void detach ()
 
bool isDetached () const
 
void setSharable (bool sharable)
 
QContiguousCache< T > & operator= (const QContiguousCache< T > &other)
 
void swap (QContiguousCache< T > &other)
 
bool operator== (const QContiguousCache< T > &other) const
 
bool operator!= (const QContiguousCache< T > &other) const
 
int capacity () const
 
int count () const
 
int size () const
 
bool isEmpty () const
 
bool isFull () const
 
int available () const
 
void clear ()
 
void setCapacity (int size)
 
const T & at (int pos) const
 
T & operator[] (int i)
 
const T & operator[] (int i) const
 
void append (const T &value)
 
void prepend (const T &value)
 
void insert (int pos, const T &value)
 
bool containsIndex (int pos) const
 
int firstIndex () const
 
int lastIndex () const
 
const T & first () const
 
const T & last () const
 
T & first ()
 
T & last ()
 
void removeFirst ()
 
takeFirst ()
 
void removeLast ()
 
takeLast ()
 
bool areIndexesValid () const
 
void normalizeIndexes ()
 

Member Typedef Documentation

typedef T value_type

Definition at line 95 of file qcontiguouscache.h.

typedef value_type* pointer

Definition at line 96 of file qcontiguouscache.h.

typedef const value_type* const_pointer

Definition at line 97 of file qcontiguouscache.h.

Definition at line 98 of file qcontiguouscache.h.

typedef const value_type& const_reference

Definition at line 99 of file qcontiguouscache.h.

typedef qptrdiff difference_type

Definition at line 100 of file qcontiguouscache.h.

typedef int size_type

Definition at line 101 of file qcontiguouscache.h.

Constructor & Destructor Documentation

QContiguousCache ( int  capacity = 0)
explicit

Definition at line 293 of file qcontiguouscache.h.

294 {
295  d = malloc(cap);
296  d->ref = 1;
297  d->alloc = cap;
298  d->count = d->start = d->offset = 0;
299  d->sharable = true;
300 }
QContiguousCacheData * d
GLenum cap
Definition: GLee.h:7211
QBasicAtomicInt ref
QContiguousCache ( const QContiguousCache< T > &  v)
inline

Definition at line 104 of file qcontiguouscache.h.

104 : d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
QContiguousCacheData * d
QBasicAtomicInt ref
~QContiguousCache ( )
inline

Definition at line 106 of file qcontiguouscache.h.

106 { if (!d) return; if (!d->ref.deref()) free(p); }
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
QBasicAtomicInt ref

Member Function Documentation

void detach ( )
inline

Definition at line 108 of file qcontiguouscache.h.

108 { if (d->ref != 1) detach_helper(); }
QContiguousCacheData * d
QBasicAtomicInt ref
bool isDetached ( ) const
inline

Definition at line 109 of file qcontiguouscache.h.

109 { return d->ref == 1; }
QContiguousCacheData * d
QBasicAtomicInt ref
void setSharable ( bool  sharable)
inline

Definition at line 110 of file qcontiguouscache.h.

110 { if (!sharable) detach(); d->sharable = sharable; }
QContiguousCacheData * d
QContiguousCache< T > & operator= ( const QContiguousCache< T > &  other)

Definition at line 303 of file qcontiguouscache.h.

304 {
305  other.d->ref.ref();
306  if (!d->ref.deref())
307  free(d);
308  d = other.d;
309  if (!d->sharable)
310  detach_helper();
311  return *this;
312 }
QContiguousCacheData * d
QBasicAtomicInt ref
void swap ( QContiguousCache< T > &  other)
inline

Definition at line 117 of file qcontiguouscache.h.

117 { qSwap(d, other.d); }
QContiguousCacheData * d
Q_INLINE_TEMPLATE void qSwap(QScopedPointer< T, Cleanup > &p1, QScopedPointer< T, Cleanup > &p2)
bool operator== ( const QContiguousCache< T > &  other) const

Definition at line 315 of file qcontiguouscache.h.

316 {
317  if (other.d == d)
318  return true;
319  if (other.d->start != d->start
320  || other.d->count != d->count
321  || other.d->offset != d->offset
322  || other.d->alloc != d->alloc)
323  return false;
324  for (int i = firstIndex(); i <= lastIndex(); ++i)
325  if (!(at(i) == other.at(i)))
326  return false;
327  return true;
328 }
QContiguousCacheData * d
int lastIndex() const
int firstIndex() const
const T & at(int pos) const
bool operator!= ( const QContiguousCache< T > &  other) const
inline

Definition at line 119 of file qcontiguouscache.h.

119 { return !(*this == other); }
int capacity ( ) const
inline

Definition at line 121 of file qcontiguouscache.h.

121 {return d->alloc; }
QContiguousCacheData * d
int count ( ) const
inline

Definition at line 122 of file qcontiguouscache.h.

122 { return d->count; }
QContiguousCacheData * d
int size ( ) const
inline

Definition at line 123 of file qcontiguouscache.h.

123 { return d->count; }
QContiguousCacheData * d
bool isEmpty ( ) const
inline

Definition at line 125 of file qcontiguouscache.h.

125 { return d->count == 0; }
QContiguousCacheData * d
bool isFull ( ) const
inline

Definition at line 126 of file qcontiguouscache.h.

126 { return d->count == d->alloc; }
QContiguousCacheData * d
int available ( ) const
inline

Definition at line 127 of file qcontiguouscache.h.

127 { return d->alloc - d->count; }
QContiguousCacheData * d
void clear ( )

Definition at line 259 of file qcontiguouscache.h.

260 {
261  if (d->ref == 1) {
262  if (QTypeInfo<T>::isComplex) {
263  int oldcount = d->count;
264  T * i = p->array + d->start;
265  T * e = p->array + d->alloc;
266  while (oldcount--) {
267  i->~T();
268  i++;
269  if (i == e)
270  i = p->array;
271  }
272  }
273  d->count = d->start = d->offset = 0;
274  } else {
276  x.d = malloc(d->alloc);
277  x.d->ref = 1;
278  x.d->alloc = d->alloc;
279  x.d->count = x.d->start = x.d->offset = 0;
280  x.d->sharable = true;
281  if (!d->ref.deref()) free(p);
282  d = x.d;
283  }
284 }
QContiguousCacheData * d
GLenum GLint x
Definition: GLee.h:876
QContiguousCacheTypedData< T > * p
GLfloat GLfloat p
Definition: GLee.h:5416
QBasicAtomicInt ref
void setCapacity ( int  size)

Definition at line 219 of file qcontiguouscache.h.

220 {
221  if (asize == d->alloc)
222  return;
223  detach();
225  x.d = malloc(asize);
226  x.d->alloc = asize;
227  x.d->count = qMin(d->count, asize);
228  x.d->offset = d->offset + d->count - x.d->count;
229  if(asize)
230  x.d->start = x.d->offset % x.d->alloc;
231  else
232  x.d->start = 0;
233 
234  int oldcount = x.d->count;
235  if(oldcount)
236  {
237  T *dest = x.p->array + (x.d->start + x.d->count-1) % x.d->alloc;
238  T *src = p->array + (d->start + d->count-1) % d->alloc;
239  while (oldcount--) {
240  if (QTypeInfo<T>::isComplex) {
241  new (dest) T(*src);
242  } else {
243  *dest = *src;
244  }
245  if (dest == x.p->array)
246  dest = x.p->array + x.d->alloc;
247  dest--;
248  if (src == p->array)
249  src = p->array + d->alloc;
250  src--;
251  }
252  }
253  /* free old */
254  free(p);
255  d = x.d;
256 }
GLuint src
Definition: GLee.h:7190
QContiguousCacheData * d
GLenum GLint x
Definition: GLee.h:876
QContiguousCacheTypedData< T > * p
GLfloat GLfloat p
Definition: GLee.h:5416
const T & at ( int  pos) const
inline

Definition at line 425 of file qcontiguouscache.h.

426 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache<T>::at", "index out of range"); return p->array[pos % d->alloc]; }
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
T & operator[] ( int  i)
inline

Definition at line 432 of file qcontiguouscache.h.

433 {
434  detach();
435  if (!containsIndex(pos))
436  insert(pos, T());
437  return p->array[pos % d->alloc];
438 }
void insert(int pos, const T &value)
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
bool containsIndex(int pos) const
const T & operator[] ( int  i) const
inline

Definition at line 428 of file qcontiguouscache.h.

429 { Q_ASSERT_X(pos >= d->offset && pos - d->offset < d->count, "QContiguousCache<T>::at", "index out of range"); return p->array[pos % d->alloc]; }
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
void append ( const T &  value)

Definition at line 347 of file qcontiguouscache.h.

348 {
349  if (!d->alloc)
350  return; // zero capacity
351  detach();
352  if (QTypeInfo<T>::isComplex) {
353  if (d->count == d->alloc)
354  (p->array + (d->start+d->count) % d->alloc)->~T();
355  new (p->array + (d->start+d->count) % d->alloc) T(value);
356  } else {
357  p->array[(d->start+d->count) % d->alloc] = value;
358  }
359 
360  if (d->count == d->alloc) {
361  d->start++;
362  d->start %= d->alloc;
363  d->offset++;
364  } else {
365  d->count++;
366  }
367 }
QContiguousCacheData * d
GLsizei const GLfloat * value
Definition: GLee.h:1742
GLfloat GLfloat p
Definition: GLee.h:5416
void prepend ( const T &  value)

Definition at line 370 of file qcontiguouscache.h.

371 {
372  if (!d->alloc)
373  return; // zero capacity
374  detach();
375  if (d->start)
376  d->start--;
377  else
378  d->start = d->alloc-1;
379  d->offset--;
380 
381  if (d->count != d->alloc)
382  d->count++;
383  else
384  if (d->count == d->alloc)
385  (p->array + d->start)->~T();
386 
387  if (QTypeInfo<T>::isComplex)
388  new (p->array + d->start) T(value);
389  else
390  p->array[d->start] = value;
391 }
QContiguousCacheData * d
GLsizei const GLfloat * value
Definition: GLee.h:1742
GLfloat GLfloat p
Definition: GLee.h:5416
void insert ( int  pos,
const T &  value 
)

Definition at line 394 of file qcontiguouscache.h.

395 {
396  Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
397  if (!d->alloc)
398  return; // zero capacity
399  detach();
400  if (containsIndex(pos)) {
401  if (QTypeInfo<T>::isComplex) {
402  (p->array + pos % d->alloc)->~T();
403  new (p->array + pos % d->alloc) T(value);
404  } else {
405  p->array[pos % d->alloc] = value;
406  }
407  } else if (pos == d->offset-1)
408  prepend(value);
409  else if (pos == d->offset+d->count)
410  append(value);
411  else {
412  // we don't leave gaps.
413  clear();
414  d->offset = pos;
415  d->start = pos % d->alloc;
416  d->count = 1;
417  if (QTypeInfo<T>::isComplex)
418  new (p->array + d->start) T(value);
419  else
420  p->array[d->start] = value;
421  }
422 }
void append(const T &value)
QContiguousCacheData * d
GLsizei const GLfloat * value
Definition: GLee.h:1742
GLfloat GLfloat p
Definition: GLee.h:5416
void prepend(const T &value)
bool containsIndex(int pos) const
bool containsIndex ( int  pos) const
inline

Definition at line 140 of file qcontiguouscache.h.

140 { return pos >= d->offset && pos - d->offset < d->count; }
QContiguousCacheData * d
int firstIndex ( ) const
inline

Definition at line 141 of file qcontiguouscache.h.

141 { return d->offset; }
QContiguousCacheData * d
int lastIndex ( ) const
inline

Definition at line 142 of file qcontiguouscache.h.

142 { return d->offset + d->count - 1; }
QContiguousCacheData * d
const T& first ( ) const
inline

Definition at line 144 of file qcontiguouscache.h.

144 { Q_ASSERT(!isEmpty()); return p->array[d->start]; }
QContiguousCacheData * d
bool isEmpty() const
GLfloat GLfloat p
Definition: GLee.h:5416
const T& last ( ) const
inline

Definition at line 145 of file qcontiguouscache.h.

145 { Q_ASSERT(!isEmpty()); return p->array[(d->start + d->count -1) % d->alloc]; }
QContiguousCacheData * d
bool isEmpty() const
GLfloat GLfloat p
Definition: GLee.h:5416
T& first ( )
inline

Definition at line 146 of file qcontiguouscache.h.

146 { Q_ASSERT(!isEmpty()); detach(); return p->array[d->start]; }
QContiguousCacheData * d
bool isEmpty() const
GLfloat GLfloat p
Definition: GLee.h:5416
T& last ( )
inline

Definition at line 147 of file qcontiguouscache.h.

147 { Q_ASSERT(!isEmpty()); detach(); return p->array[(d->start + d->count -1) % d->alloc]; }
QContiguousCacheData * d
bool isEmpty() const
GLfloat GLfloat p
Definition: GLee.h:5416
void removeFirst ( )
inline

Definition at line 441 of file qcontiguouscache.h.

442 {
443  Q_ASSERT(d->count > 0);
444  detach();
445  d->count--;
446  if (QTypeInfo<T>::isComplex)
447  (p->array + d->start)->~T();
448  d->start = (d->start + 1) % d->alloc;
449  d->offset++;
450 }
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
T takeFirst ( )
inline

Definition at line 463 of file qcontiguouscache.h.

464 { T t = first(); removeFirst(); return t; }
const T & first() const
GLdouble GLdouble t
Definition: GLee.h:1181
void removeLast ( )
inline

Definition at line 453 of file qcontiguouscache.h.

454 {
455  Q_ASSERT(d->count > 0);
456  detach();
457  d->count--;
458  if (QTypeInfo<T>::isComplex)
459  (p->array + (d->start + d->count) % d->alloc)->~T();
460 }
QContiguousCacheData * d
GLfloat GLfloat p
Definition: GLee.h:5416
T takeLast ( )
inline

Definition at line 467 of file qcontiguouscache.h.

468 { T t = last(); removeLast(); return t; }
const T & last() const
GLdouble GLdouble t
Definition: GLee.h:1181
bool areIndexesValid ( ) const
inline

Definition at line 154 of file qcontiguouscache.h.

155  { return d->offset >= 0 && d->offset < INT_MAX - d->count && (d->offset % d->alloc) == d->start; }
QContiguousCacheData * d
void normalizeIndexes ( )
inline

Definition at line 157 of file qcontiguouscache.h.

157 { d->offset = d->start; }
QContiguousCacheData * d

Member Data Documentation

Definition at line 92 of file qcontiguouscache.h.

Definition at line 92 of file qcontiguouscache.h.


The documentation for this class was generated from the following file: