Classes | Public Types | Public Member Functions | Static Public Member Functions | Friends

QList< T > Class Template Reference

Search for all occurrences

Detailed Description

template<typename T>
class QList< T >

Definition at line 102 of file qlist.h.

#include <qlist.h>

Inheritance diagram for QList< T >:
Inheritance graph
[legend]

List of all members.

Classes

class   const_iterator
class   iterator
struct   Node

Public Types

typedef iterator  Iterator
typedef const_iterator  ConstIterator
typedef int  size_type
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

Public Member Functions

  QList ()
  QList (const QList< T > &l)
  ~QList ()
QList< T > &  operator= (const QList< T > &l)
bool  operator== (const QList< T > &l) const
bool  operator!= (const QList< T > &l) const
int  size () const
void  detach ()
void  detachShared ()
bool  isDetached () const
void  setSharable (bool sharable)
bool  isSharedWith (const QList< T > &other) const
bool  isEmpty () const
void  clear ()
const T &  at (int i) const
const T &  operator[] (int i) const
T &  operator[] (int i)
void  reserve (int size)
void  append (const T &t)
void  append (const QList< T > &t)
void  prepend (const T &t)
void  insert (int i, const T &t)
void  replace (int i, const T &t)
void  removeAt (int i)
int  removeAll (const T &t)
bool  removeOne (const T &t)
takeAt (int i)
takeFirst ()
takeLast ()
void  move (int from, int to)
void  swap (int i, int j)
int  indexOf (const T &t, int from=0) const
int  lastIndexOf (const T &t, int from=-1) const
QBool  contains (const T &t) const
int  count (const T &t) const
iterator  begin ()
const_iterator  begin () const
const_iterator  constBegin () const
iterator  end ()
const_iterator  end () const
const_iterator  constEnd () const
iterator  insert (iterator before, const T &t)
iterator  erase (iterator pos)
iterator  erase (iterator first, iterator last)
int  count () const
int  length () const
T &  first ()
const T &  first () const
T &  last ()
const T &  last () const
void  removeFirst ()
void  removeLast ()
bool  startsWith (const T &t) const
bool  endsWith (const T &t) const
QList< T >  mid (int pos, int length=-1) const
value (int i) const
value (int i, const T &defaultValue) const
void  push_back (const T &t)
void  push_front (const T &t)
T &  front ()
const T &  front () const
T &  back ()
const T &  back () const
void  pop_front ()
void  pop_back ()
bool  empty () const
QList< T > &  operator+= (const QList< T > &l)
QList< T >  operator+ (const QList< T > &l) const
QList< T > &  operator+= (const T &t)
QList< T > &  operator<< (const T &t)
QList< T > &  operator<< (const QList< T > &l)
QVector< T >  toVector () const
QSet< T >  toSet () const
std::list< T >  toStdList () const

Static Public Member Functions

static QList< T >  fromVector (const QVector< T > &vector)
static QList< T >  fromSet (const QSet< T > &set)
static QList< T >  fromStdList (const std::list< T > &list)

Friends

class  iterator
class  const_iterator

Member Typedef Documentation

typedef iterator Iterator

Definition at line 265 of file qlist.h.

Definition at line 266 of file qlist.h.

typedef int size_type

Definition at line 292 of file qlist.h.

typedef T value_type

Definition at line 293 of file qlist.h.

typedef value_type* pointer

Definition at line 294 of file qlist.h.

typedef const value_type* const_pointer

Definition at line 295 of file qlist.h.

Definition at line 296 of file qlist.h.

typedef const value_type& const_reference

Definition at line 297 of file qlist.h.

typedef qptrdiff difference_type

Definition at line 298 of file qlist.h.


Constructor & Destructor Documentation

QList ( ) [inline]

Definition at line 117 of file qlist.h.

QList ( const QList< T > &  l ) [inline]

Definition at line 118 of file qlist.h.

: d(l.d) { d->ref.ref(); if (!d->sharable) detach_helper(); }
Q_OUTOFLINE_TEMPLATE ~QList ( )

Definition at line 714 of file qlist.h.

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

Member Function Documentation

Q_INLINE_TEMPLATE QList< T > & operator= ( const QList< T > &  l )

Definition at line 424 of file qlist.h.

{
    if (d != l.d) {
        QListData::Data *o = l.d;
        o->ref.ref();
        if (!d->ref.deref())
            free(d);
        d = o;
        if (!d->sharable)
            detach_helper();
    }
    return *this;
}
Q_OUTOFLINE_TEMPLATE bool operator== ( const QList< T > &  l ) const

Definition at line 721 of file qlist.h.

{
    if (p.size() != l.p.size())
        return false;
    if (d == l.d)
        return true;
    Node *i = reinterpret_cast<Node *>(p.end());
    Node *b = reinterpret_cast<Node *>(p.begin());
    Node *li = reinterpret_cast<Node *>(l.p.end());
    while (i != b) {
        --i; --li;
        if (!(i->t() == li->t()))
            return false;
    }
    return true;
}
bool operator!= ( const QList< T > &  l ) const [inline]

Definition at line 122 of file qlist.h.

{ return !(*this == l); }
int size ( ) const [inline]

Definition at line 124 of file qlist.h.

{ return p.size(); }
void detach ( ) [inline]

Definition at line 126 of file qlist.h.

{ if (d->ref != 1) detach_helper(); }
void detachShared ( ) [inline]

Definition at line 128 of file qlist.h.

    {
        // The "this->" qualification is needed for GCCE.
        if (d->ref != 1 && this->d != &QListData::shared_null)
            detach_helper();
    }
bool isDetached ( ) const [inline]

Definition at line 135 of file qlist.h.

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

Definition at line 136 of file qlist.h.

{ if (!sharable) detach(); d->sharable = sharable; }
bool isSharedWith ( const QList< T > &  other ) const [inline]

Definition at line 137 of file qlist.h.

{ return d == other.d; }
bool isEmpty ( ) const [inline]

Definition at line 139 of file qlist.h.

{ return p.isEmpty(); }
Q_OUTOFLINE_TEMPLATE void clear ( )

Definition at line 750 of file qlist.h.

{
    *this = QList<T>();
}
const T & at ( int  i ) const [inline]

Definition at line 455 of file qlist.h.

{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range");
 return reinterpret_cast<Node *>(p.at(i))->t(); }
const T & operator[] ( int  i ) const [inline]

Definition at line 459 of file qlist.h.

{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
 return reinterpret_cast<Node *>(p.at(i))->t(); }
T & operator[] ( int  i ) [inline]

Definition at line 463 of file qlist.h.

{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::operator[]", "index out of range");
  detach(); return reinterpret_cast<Node *>(p.at(i))->t(); }
Q_OUTOFLINE_TEMPLATE void reserve ( int  size )

Definition at line 483 of file qlist.h.

{
    if (d->alloc < alloc) {
        if (d->ref != 1)
            detach_helper(alloc);
        else
            p.realloc(alloc);
    }
}
Q_OUTOFLINE_TEMPLATE void append ( const T &  t )

Definition at line 494 of file qlist.h.

{
    if (d->ref != 1) {
        Node *n = detach_helper_grow(INT_MAX, 1);
        QT_TRY {
            node_construct(n, t);
        } QT_CATCH(...) {
            --d->end;
            QT_RETHROW;
        }
    } else {
        if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
            Node *n = reinterpret_cast<Node *>(p.append());
            QT_TRY {
                node_construct(n, t);
            } QT_CATCH(...) {
                --d->end;
                QT_RETHROW;
            }
        } else {
            Node *n, copy;
            node_construct(&copy, t); // t might be a reference to an object in the array
            QT_TRY {
                n = reinterpret_cast<Node *>(p.append());;
            } QT_CATCH(...) {
                node_destruct(&copy);
                QT_RETHROW;
            }
            *n = copy;
        }
    }
}
void append ( const QList< T > &  t ) [inline]

Definition at line 820 of file qlist.h.

{
    *this += t;
}
void prepend ( const T &  t ) [inline]

Definition at line 528 of file qlist.h.

{
    if (d->ref != 1) {
        Node *n = detach_helper_grow(0, 1);
        QT_TRY {
            node_construct(n, t);
        } QT_CATCH(...) {
            ++d->begin;
            QT_RETHROW;
        }
    } else {
        if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
            Node *n = reinterpret_cast<Node *>(p.prepend());
            QT_TRY {
                node_construct(n, t);
            } QT_CATCH(...) {
                ++d->begin;
                QT_RETHROW;
            }
        } else {
            Node *n, copy;
            node_construct(&copy, t); // t might be a reference to an object in the array
            QT_TRY {
                n = reinterpret_cast<Node *>(p.prepend());;
            } QT_CATCH(...) {
                node_destruct(&copy);
                QT_RETHROW;
            }
            *n = copy;
        }
    }
}
void insert ( int  i,
const T &  t 
) [inline]

Definition at line 562 of file qlist.h.

{
    if (d->ref != 1) {
        Node *n = detach_helper_grow(i, 1);
        QT_TRY {
            node_construct(n, t);
        } QT_CATCH(...) {
            p.remove(i);
            QT_RETHROW;
        }
    } else {
        if (QTypeInfo<T>::isLarge || QTypeInfo<T>::isStatic) {
            Node *n = reinterpret_cast<Node *>(p.insert(i));
            QT_TRY {
                node_construct(n, t);
            } QT_CATCH(...) {
                p.remove(i);
                QT_RETHROW;
            }
        } else {
            Node *n, copy;
            node_construct(&copy, t); // t might be a reference to an object in the array
            QT_TRY {
                n = reinterpret_cast<Node *>(p.insert(i));;
            } QT_CATCH(...) {
                node_destruct(&copy);
                QT_RETHROW;
            }
            *n = copy;
        }
    }
}
void replace ( int  i,
const T &  t 
) [inline]

Definition at line 596 of file qlist.h.

{
    Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::replace", "index out of range");
    detach();
    reinterpret_cast<Node *>(p.at(i))->t() = t;
}
void removeAt ( int  i ) [inline]

Definition at line 467 of file qlist.h.

{ if(i >= 0 && i < p.size()) { detach();
 node_destruct(reinterpret_cast<Node *>(p.at(i))); p.remove(i); } }
Q_OUTOFLINE_TEMPLATE int removeAll ( const T &  t )

Definition at line 756 of file qlist.h.

{
    detachShared();
    const T t = _t;
    int removedCount=0, i=0;
    Node *n;
    while (i < p.size())
        if ((n = reinterpret_cast<Node *>(p.at(i)))->t() == t) {
            node_destruct(n);
            p.remove(i);
            ++removedCount;
        } else {
            ++i;
        }
    return removedCount;
}
Q_OUTOFLINE_TEMPLATE bool removeOne ( const T &  t )

Definition at line 774 of file qlist.h.

{
    detachShared();
    int index = indexOf(_t);
    if (index != -1) {
        removeAt(index);
        return true;
    }
    return false;
}
T takeAt ( int  i ) [inline]

Definition at line 471 of file qlist.h.

{ Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::take", "index out of range");
 detach(); Node *n = reinterpret_cast<Node *>(p.at(i)); T t = n->t(); node_destruct(n);
 p.remove(i); return t; }
T takeFirst ( ) [inline]

Definition at line 476 of file qlist.h.

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

Definition at line 479 of file qlist.h.

{ T t = last(); removeLast(); return t; }
void move ( int  from,
int  to 
) [inline]

Definition at line 615 of file qlist.h.

{
    Q_ASSERT_X(from >= 0 && from < p.size() && to >= 0 && to < p.size(),
               "QList<T>::move", "index out of range");
    detach();
    p.move(from, to);
}
void swap ( int  i,
int  j 
) [inline]

Definition at line 604 of file qlist.h.

{
    Q_ASSERT_X(i >= 0 && i < p.size() && j >= 0 && j < p.size(),
                "QList<T>::swap", "index out of range");
    detach();
    void *t = d->array[d->begin + i];
    d->array[d->begin + i] = d->array[d->begin + j];
    d->array[d->begin + j] = t;
}
Q_OUTOFLINE_TEMPLATE int indexOf ( const T &  t,
int  from = 0 
) const

Definition at line 826 of file qlist.h.

{
    if (from < 0)
        from = qMax(from + p.size(), 0);
    if (from < p.size()) {
        Node *n = reinterpret_cast<Node *>(p.at(from -1));
        Node *e = reinterpret_cast<Node *>(p.end());
        while (++n != e)
            if (n->t() == t)
                return int(n - reinterpret_cast<Node *>(p.begin()));
    }
    return -1;
}
Q_OUTOFLINE_TEMPLATE int lastIndexOf ( const T &  t,
int  from = -1 
) const

Definition at line 841 of file qlist.h.

{
    if (from < 0)
        from += p.size();
    else if (from >= p.size())
        from = p.size()-1;
    if (from >= 0) {
        Node *b = reinterpret_cast<Node *>(p.begin());
        Node *n = reinterpret_cast<Node *>(p.at(from + 1));
        while (n-- != b) {
            if (n->t() == t)
                return n - b;
        }
    }
    return -1;
}
Q_OUTOFLINE_TEMPLATE QBool contains ( const T &  t ) const

Definition at line 859 of file qlist.h.

{
    Node *b = reinterpret_cast<Node *>(p.begin());
    Node *i = reinterpret_cast<Node *>(p.end());
    while (i-- != b)
        if (i->t() == t)
            return QBool(true);
    return QBool(false);
}
Q_OUTOFLINE_TEMPLATE int count ( const T &  t ) const

Definition at line 870 of file qlist.h.

{
    int c = 0;
    Node *b = reinterpret_cast<Node *>(p.begin());
    Node *i = reinterpret_cast<Node *>(p.end());
    while (i-- != b)
        if (i->t() == t)
            ++c;
    return c;
}
iterator begin ( ) [inline]

Definition at line 254 of file qlist.h.

{ detach(); return reinterpret_cast<Node *>(p.begin()); }
const_iterator begin ( ) const [inline]

Definition at line 255 of file qlist.h.

{ return reinterpret_cast<Node *>(p.begin()); }
const_iterator constBegin ( ) const [inline]

Definition at line 256 of file qlist.h.

{ return reinterpret_cast<Node *>(p.begin()); }
iterator end ( ) [inline]

Definition at line 257 of file qlist.h.

{ detach(); return reinterpret_cast<Node *>(p.end()); }
const_iterator end ( ) const [inline]

Definition at line 258 of file qlist.h.

{ return reinterpret_cast<Node *>(p.end()); }
const_iterator constEnd ( ) const [inline]

Definition at line 259 of file qlist.h.

{ return reinterpret_cast<Node *>(p.end()); }
QList< T >::iterator insert ( iterator  before,
const T &  t 
) [inline]

Definition at line 438 of file qlist.h.

{
    int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin()));
    Node *n = reinterpret_cast<Node *>(p.insert(iBefore));
    QT_TRY {
        node_construct(n, t);
    } QT_CATCH(...) {
        p.remove(iBefore);
        QT_RETHROW;
    }
    return n;
}
QList< T >::iterator erase ( iterator  pos ) [inline]

Definition at line 451 of file qlist.h.

{ node_destruct(it.i);
 return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); }
iterator erase ( iterator  first,
iterator  last 
)
int count ( ) const [inline]

Definition at line 267 of file qlist.h.

{ return p.size(); }
int length ( ) const [inline]

Definition at line 268 of file qlist.h.

{ return p.size(); } // Same as count()
T& first ( ) [inline]

Definition at line 269 of file qlist.h.

{ Q_ASSERT(!isEmpty()); return *begin(); }
const T& first ( ) const [inline]

Definition at line 270 of file qlist.h.

{ Q_ASSERT(!isEmpty()); return at(0); }
T& last ( ) [inline]

Definition at line 271 of file qlist.h.

{ Q_ASSERT(!isEmpty()); return *(--end()); }
const T& last ( ) const [inline]

Definition at line 272 of file qlist.h.

{ Q_ASSERT(!isEmpty()); return at(count() - 1); }
void removeFirst ( ) [inline]

Definition at line 273 of file qlist.h.

{ Q_ASSERT(!isEmpty()); erase(begin()); }
void removeLast ( ) [inline]

Definition at line 274 of file qlist.h.

{ Q_ASSERT(!isEmpty()); erase(--end()); }
bool startsWith ( const T &  t ) const [inline]

Definition at line 275 of file qlist.h.

{ return !isEmpty() && first() == t; }
bool endsWith ( const T &  t ) const [inline]

Definition at line 276 of file qlist.h.

{ return !isEmpty() && last() == t; }
Q_OUTOFLINE_TEMPLATE QList< T > mid ( int  pos,
int  length = -1 
) const

Definition at line 624 of file qlist.h.

{
    if (alength < 0 || pos + alength > size())
        alength = size() - pos;
    if (pos == 0 && alength == size())
        return *this;
    QList<T> cpy;
    cpy.reserve(alength);
    cpy.d->end = alength;
    QT_TRY {
        cpy.node_copy(reinterpret_cast<Node *>(cpy.p.begin()),
                      reinterpret_cast<Node *>(cpy.p.end()),
                      reinterpret_cast<Node *>(p.begin() + pos));
    } QT_CATCH(...) {
        // restore the old end
        cpy.d->end = 0;
        QT_RETHROW;
    }
    return cpy;
}
Q_OUTOFLINE_TEMPLATE T value ( int  i ) const

Definition at line 646 of file qlist.h.

{
    if (i < 0 || i >= p.size()) {
        return T();
    }
    return reinterpret_cast<Node *>(p.at(i))->t();
}
Q_OUTOFLINE_TEMPLATE T value ( int  i,
const T &  defaultValue 
) const

Definition at line 655 of file qlist.h.

{
    return ((i < 0 || i >= p.size()) ? defaultValue : reinterpret_cast<Node *>(p.at(i))->t());
}
void push_back ( const T &  t ) [inline]

Definition at line 283 of file qlist.h.

{ append(t); }
void push_front ( const T &  t ) [inline]

Definition at line 284 of file qlist.h.

{ prepend(t); }
T& front ( ) [inline]

Definition at line 285 of file qlist.h.

{ return first(); }
const T& front ( ) const [inline]

Definition at line 286 of file qlist.h.

{ return first(); }
T& back ( ) [inline]

Definition at line 287 of file qlist.h.

{ return last(); }
const T& back ( ) const [inline]

Definition at line 288 of file qlist.h.

{ return last(); }
void pop_front ( ) [inline]

Definition at line 289 of file qlist.h.

{ removeFirst(); }
void pop_back ( ) [inline]

Definition at line 290 of file qlist.h.

{ removeLast(); }
bool empty ( ) const [inline]

Definition at line 291 of file qlist.h.

{ return isEmpty(); }
Q_OUTOFLINE_TEMPLATE QList< T > & operator+= ( const QList< T > &  l )

Definition at line 797 of file qlist.h.

{
    if (!l.isEmpty()) {
        if (isEmpty()) {
            *this = l;
        } else {
            Node *n = (d->ref != 1)
                      ? detach_helper_grow(INT_MAX, l.size())
                      : reinterpret_cast<Node *>(p.append2(l.p));
            QT_TRY {
                node_copy(n, reinterpret_cast<Node *>(p.end()),
                          reinterpret_cast<Node *>(l.p.begin()));
            } QT_CATCH(...) {
                // restore the old end
                d->end -= int(reinterpret_cast<Node *>(p.end()) - n);
                QT_RETHROW;
            }
        }
    }
    return *this;
}
QList<T> operator+ ( const QList< T > &  l ) const [inline]

Definition at line 316 of file qlist.h.

    { QList n = *this; n += l; return n; }
QList<T>& operator+= ( const T &  t ) [inline]

Definition at line 318 of file qlist.h.

    { append(t); return *this; }
QList<T>& operator<< ( const T &  t ) [inline]

Reimplemented in QStringList.

Definition at line 320 of file qlist.h.

    { append(t); return *this; }
QList<T>& operator<< ( const QList< T > &  l ) [inline]

Definition at line 322 of file qlist.h.

    { *this += l; return *this; }
Q_OUTOFLINE_TEMPLATE QVector< T > toVector ( ) const

Definition at line 752 of file qvector.h.

{
    QVector<T> result(size());
    for (int i = 0; i < size(); ++i)
        result[i] = at(i);
    return result;
}
Q_OUTOFLINE_TEMPLATE QSet< T > toSet ( ) const

Definition at line 304 of file qset.h.

{
    QSet<T> result;
    result.reserve(size());
    for (int i = 0; i < size(); ++i)
        result.insert(at(i));
    return result;
}
QList< T > fromVector ( const QVector< T > &  vector ) [static]

Definition at line 767 of file qvector.h.

{
    return vector.toList();
}
QList< T > fromSet ( const QSet< T > &  set ) [static]

Definition at line 320 of file qset.h.

{
    return set.toList();
}
static QList<T> fromStdList ( const std::list< T > &  list ) [inline, static]

Definition at line 332 of file qlist.h.

    { QList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
std::list<T> toStdList ( ) const [inline]

Definition at line 334 of file qlist.h.

    { std::list<T> tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }

Friends And Related Function Documentation

friend class iterator [friend]

Definition at line 213 of file qlist.h.

friend class const_iterator [friend]

Definition at line 251 of file qlist.h.


Member Data Documentation

Definition at line 114 of file qlist.h.

Definition at line 114 of file qlist.h.


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