Public Types | Public Member Functions

QContiguousCache< T > Class Template Reference

Search for all occurrences

Detailed Description

template<typename T>
class QContiguousCache< T >

Definition at line 90 of file qcontiguouscache.h.

#include <qcontiguouscache.h>

List of all members.

Public Types

typedef T  value_type
typedef value_type pointer
typedef const value_type const_pointer
typedef value_type reference
typedef const value_type const_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)
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 288 of file qcontiguouscache.h.

{
    d = malloc(cap);
    d->ref = 1;
    d->alloc = cap;
    d->count = d->start = d->offset = 0;
    d->sharable = true;
}
QContiguousCache ( const QContiguousCache< T > &  v ) [inline]

Definition at line 104 of file qcontiguouscache.h.

: d(v.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
~QContiguousCache ( ) [inline]

Definition at line 106 of file qcontiguouscache.h.

{ if (!d) return; if (!d->ref.deref()) free(p); }

Member Function Documentation

void detach ( ) [inline]

Definition at line 108 of file qcontiguouscache.h.

{ if (d->ref != 1) detach_helper(); }
bool isDetached ( ) const [inline]

Definition at line 109 of file qcontiguouscache.h.

{ return d->ref == 1; }
void setSharable ( bool  sharable ) [inline]

Definition at line 110 of file qcontiguouscache.h.

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

Definition at line 298 of file qcontiguouscache.h.

{
    other.d->ref.ref();
    if (!d->ref.deref())
        free(d);
    d = other.d;
    if (!d->sharable)
        detach_helper();
    return *this;
}
bool operator== ( const QContiguousCache< T > &  other ) const

Definition at line 310 of file qcontiguouscache.h.

{
    if (other.d == d)
        return true;
    if (other.d->start != d->start
            || other.d->count != d->count
            || other.d->offset != d->offset
            || other.d->alloc != d->alloc)
        return false;
    for (int i = firstIndex(); i <= lastIndex(); ++i)
        if (!(at(i) == other.at(i)))
            return false;
    return true;
}
bool operator!= ( const QContiguousCache< T > &  other ) const [inline]

Definition at line 114 of file qcontiguouscache.h.

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

Definition at line 116 of file qcontiguouscache.h.

{return d->alloc; }
int count ( ) const [inline]

Definition at line 117 of file qcontiguouscache.h.

{ return d->count; }
int size ( ) const [inline]

Definition at line 118 of file qcontiguouscache.h.

{ return d->count; }
bool isEmpty ( ) const [inline]

Definition at line 120 of file qcontiguouscache.h.

{ return d->count == 0; }
bool isFull ( ) const [inline]

Definition at line 121 of file qcontiguouscache.h.

{ return d->count == d->alloc; }
int available ( ) const [inline]

Definition at line 122 of file qcontiguouscache.h.

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

Definition at line 254 of file qcontiguouscache.h.

{
    if (d->ref == 1) {
        if (QTypeInfo<T>::isComplex) {
            int oldcount = d->count;
            T * i = p->array + d->start;
            T * e = p->array + d->alloc;
            while (oldcount--) {
                i->~T();
                i++;
                if (i == e)
                    i = p->array;
            }
        }
        d->count = d->start = d->offset = 0;
    } else {
        union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
        x.d = malloc(d->alloc);
        x.d->ref = 1;
        x.d->alloc = d->alloc;
        x.d->count = x.d->start = x.d->offset = 0;
        x.d->sharable = true;
        if (!d->ref.deref()) free(p);
        d = x.d;
    }
}
void setCapacity ( int  size )

Definition at line 214 of file qcontiguouscache.h.

{
    if (asize == d->alloc)
        return;
    detach();
    union { QContiguousCacheData *d; QContiguousCacheTypedData<T> *p; } x;
    x.d = malloc(asize);
    x.d->alloc = asize;
    x.d->count = qMin(d->count, asize);
    x.d->offset = d->offset + d->count - x.d->count;
    if(asize)
        x.d->start = x.d->offset % x.d->alloc;
    else
        x.d->start = 0;

    int oldcount = x.d->count;
    if(oldcount)
    {
        T *dest = x.p->array + (x.d->start + x.d->count-1) % x.d->alloc;
        T *src = p->array + (d->start + d->count-1) % d->alloc;
        while (oldcount--) {
            if (QTypeInfo<T>::isComplex) {
                new (dest) T(*src);
            } else {
                *dest = *src;
            }
            if (dest == x.p->array)
                dest = x.p->array + x.d->alloc;
            dest--;
            if (src == p->array)
                src = p->array + d->alloc;
            src--;
        }
    }
    /* free old */
    free(p);
    d = x.d;
}
const T & at ( int  pos ) const [inline]

Definition at line 412 of file qcontiguouscache.h.

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

Definition at line 419 of file qcontiguouscache.h.

{
    detach();
    if (!containsIndex(pos))
        insert(pos, T());
    return p->array[pos % d->alloc];
}
const T & operator[] ( int  i ) const [inline]

Definition at line 415 of file qcontiguouscache.h.

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

Definition at line 342 of file qcontiguouscache.h.

{
    detach();
    if (QTypeInfo<T>::isComplex) {
        if (d->count == d->alloc)
            (p->array + (d->start+d->count) % d->alloc)->~T();
        new (p->array + (d->start+d->count) % d->alloc) T(value);
    } else {
        p->array[(d->start+d->count) % d->alloc] = value;
    }

    if (d->count == d->alloc) {
        d->start++;
        d->start %= d->alloc;
        d->offset++;
    } else {
        d->count++;
    }
}
void prepend ( const T &  value )

Definition at line 363 of file qcontiguouscache.h.

{
    detach();
    if (d->start)
        d->start--;
    else
        d->start = d->alloc-1;
    d->offset--;

    if (d->count != d->alloc)
        d->count++;
    else
        if (d->count == d->alloc)
            (p->array + d->start)->~T();

    if (QTypeInfo<T>::isComplex)
        new (p->array + d->start) T(value);
    else
        p->array[d->start] = value;
}
void insert ( int  pos,
const T &  value 
)

Definition at line 385 of file qcontiguouscache.h.

{
    Q_ASSERT_X(pos >= 0 && pos < INT_MAX, "QContiguousCache<T>::insert", "index out of range");
    detach();
    if (containsIndex(pos)) {
        if(QTypeInfo<T>::isComplex)
            new (p->array + pos % d->alloc) T(value);
        else
            p->array[pos % d->alloc] = value;
    } else if (pos == d->offset-1)
        prepend(value);
    else if (pos == d->offset+d->count)
        append(value);
    else {
        // we don't leave gaps.
        clear();
        d->offset = pos;
        d->start = pos % d->alloc;
        d->count = 1;
        if (QTypeInfo<T>::isComplex)
            new (p->array + d->start) T(value);
        else
            p->array[d->start] = value;
    }
}
bool containsIndex ( int  pos ) const [inline]

Definition at line 135 of file qcontiguouscache.h.

{ return pos >= d->offset && pos - d->offset < d->count; }
int firstIndex ( ) const [inline]

Definition at line 136 of file qcontiguouscache.h.

{ return d->offset; }
int lastIndex ( ) const [inline]

Definition at line 137 of file qcontiguouscache.h.

{ return d->offset + d->count - 1; }
const T& first ( ) const [inline]

Definition at line 139 of file qcontiguouscache.h.

{ Q_ASSERT(!isEmpty()); return p->array[d->start]; }
const T& last ( ) const [inline]

Definition at line 140 of file qcontiguouscache.h.

{ Q_ASSERT(!isEmpty()); return p->array[(d->start + d->count -1) % d->alloc]; }
T& first ( ) [inline]

Definition at line 141 of file qcontiguouscache.h.

{ Q_ASSERT(!isEmpty()); detach(); return p->array[d->start]; }
T& last ( ) [inline]

Definition at line 142 of file qcontiguouscache.h.

{ Q_ASSERT(!isEmpty()); detach(); return p->array[(d->start + d->count -1) % d->alloc]; }
void removeFirst ( ) [inline]

Definition at line 428 of file qcontiguouscache.h.

{
    Q_ASSERT(d->count > 0);
    detach();
    d->count--;
    if (QTypeInfo<T>::isComplex)
        (p->array + d->start)->~T();
    d->start = (d->start + 1) % d->alloc;
    d->offset++;
}
T takeFirst ( ) [inline]

Definition at line 450 of file qcontiguouscache.h.

{ T t = first(); removeFirst(); return t; }
void removeLast ( ) [inline]

Definition at line 440 of file qcontiguouscache.h.

{
    Q_ASSERT(d->count > 0);
    detach();
    d->count--;
    if (QTypeInfo<T>::isComplex)
        (p->array + (d->start + d->count) % d->alloc)->~T();
}
T takeLast ( ) [inline]

Definition at line 454 of file qcontiguouscache.h.

{ T t = last(); removeLast(); return t; }
bool areIndexesValid ( ) const [inline]

Definition at line 149 of file qcontiguouscache.h.

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

Definition at line 152 of file qcontiguouscache.h.

{ d->offset = d->start; }

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: