QLinkedList< T > Class Template Reference

#include <qlinkedlist.h>

Inheritance diagram for QLinkedList< T >:

Inheritance graph
[legend]
Collaboration diagram for QLinkedList< T >:

Collaboration graph
[legend]
List of all members.

Detailed Description

template<class T>
class QLinkedList< T >

The QLinkedList class is a template class that provides linked lists.

QLinkedList<T> is one of Qt's generic {container classes}. It stores a list of values and provides iterator-based access as well as {constant time} insertions and removals.

QList<T>, QLinkedList<T>, and QVector<T> provide similar functionality. Here's an overview:

For most purposes, QList is the right class to use. Its index-based API is more convenient than QLinkedList's iterator-based API, and it is usually faster than QVector because of the way it stores its items in memory (see {Algorithmic Complexity} for details). It also expands to less code in your executable. If you need a real linked list, with guarantees of {constant time} insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList. If you want the items to occupy adjacent memory positions, use QVector.

Here's an example of a QLinkedList that stores integers and a QLinkedList that stores QTime values:

        QLinkedList<int> integerList;
        QLinkedList<QTime> timeList;

QLinkedList stores a list of items. The default constructor creates an empty list. To insert items into the list, you can use operator<<():

        QLinkedList<QString> list;
        list << "one" << "two" << "three";
        // list: ["one", "two", "three"]

If you want to get the first or last item in a linked list, use first() or last(). If you want to remove an item from either end of the list, use removeFirst() or removeLast(). If you want to remove all occurrences of a given value in the list, use removeAll().

A common requirement is to remove the first or last item in the list and do something with it. For this, QLinkedList provides takeFirst() and takeLast(). Here's a loop that removes the items from a list one at a time and calls delete on them:

        QLinkedList<QWidget *> list;
        ...
        while (!list.isEmpty())
            delete list.takeFirst();

QLinkedList's value type must be an {assignable data type}. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, contains() and removeAll() expect the value type to support operator==(). These requirements are documented on a per-function basis.

If you want to insert, modify, or remove items in the middle of the list, you must use an iterator. QLinkedList provides both {Java-style iterators} (QLinkedListIterator and QMutableLinkedListIterator) and {STL-style iterators} (QLinkedList::const_iterator and QLinkedList::iterator). See the documentation for these classes for details.

See also:
QListIterator, QMutableListIterator, QList, QVector

Definition at line 58 of file qlinkedlist.h.

Public Types

typedef iterator Iterator
typedef const_iterator ConstIterator
typedef int size_type
typedef T value_type
typedef value_typepointer
typedef const value_typeconst_pointer
typedef value_typereference
typedef const value_typeconst_reference
typedef ptrdiff_t difference_type

Public Member Functions

 QLinkedList ()
 QLinkedList (const QLinkedList< T > &l)
 ~QLinkedList ()
QLinkedList< T > & operator= (const QLinkedList< T > &)
bool operator== (const QLinkedList< T > &l) const
bool operator!= (const QLinkedList< T > &l) const
int size () const
void detach ()
bool isDetached () const
void setSharable (bool sharable)
bool isEmpty () const
void clear ()
void append (const T &)
void prepend (const T &)
takeFirst ()
takeLast ()
int removeAll (const T &t)
bool 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
T & first ()
const T & first () const
T & last ()
const T & last () const
void removeFirst ()
void removeLast ()
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
std::list< T > toStdList () const
QLinkedList< T > & operator+= (const QLinkedList< T > &l)
QLinkedList< T > operator+ (const QLinkedList< T > &l) const
QLinkedList< T > & operator+= (const T &t)
QLinkedList< T > & operator<< (const T &t)
QLinkedList< T > & operator<< (const QLinkedList< T > &l)

Static Public Member Functions

static QLinkedList< T > fromStdList (const std::list< T > &list)

Private Types

typedef QLinkedListNode< T > Node

Private Member Functions

void detach_helper ()
void free (QLinkedListData *)

Private Attributes

union {
   QLinkedListData *   d
   QLinkedListNode< T > *   e
}; 

Friends

class iterator
class const_iterator

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QLinkedList< T > &list)
QDataStreamoperator>> (QDataStream &in, QLinkedList< T > &list)

Classes

class  const_iterator
 The QLinkedList::const_iterator class provides an STL-style const iterator for QLinkedList. More...
class  iterator
 The QLinkedList::iterator class provides an STL-style non-const iterator for QLinkedList. More...


Member Typedef Documentation

template<class T>
typedef QLinkedListNode<T> QLinkedList< T >::Node [private]

Definition at line 60 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::Iterator

Qt-style synonym for QList::iterator.

Definition at line 166 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::ConstIterator

Qt-style synonym for QList::const_iterator.

Definition at line 167 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::size_type

Typedef for int. Provided for STL compatibility.

Definition at line 186 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::value_type

Typedef for T. Provided for STL compatibility.

Definition at line 187 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::pointer

Typedef for T *. Provided for STL compatibility.

Definition at line 188 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::const_pointer

Typedef for const T *. Provided for STL compatibility.

Definition at line 189 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::reference

Typedef for T &. Provided for STL compatibility.

Definition at line 190 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::const_reference

Typedef for const T &. Provided for STL compatibility.

Definition at line 191 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::difference_type

Typedef for ptrdiff_t. Provided for STL compatibility.

Definition at line 192 of file qlinkedlist.h.


Constructor & Destructor Documentation

template<class T>
QLinkedList< T >::QLinkedList (  )  [inline]

Constructs an empty list.

Definition at line 64 of file qlinkedlist.h.

template<class T>
QLinkedList< T >::QLinkedList ( const QLinkedList< T > &  other  )  [inline]

Constructs a copy of other.

This operation occurs in {constant time}, because QLinkedList is {implicitly shared}. This makes returning a QLinkedList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and this takes {linear time}.

See also:
operator=()

Definition at line 65 of file qlinkedlist.h.

00065 : d(l.d) { d->ref.ref(); if (!d->sharable) detach(); }

template<typename T>
QLinkedList< T >::~QLinkedList (  )  [inline]

Destroys the list. References to the values in the list, and all iterators over this list, become invalid.

Definition at line 229 of file qlinkedlist.h.

References QLinkedList< T >::d, QBasicAtomic::deref(), QLinkedList< T >::free(), and QLinkedListData::ref.

00230 {
00231     if (!d)
00232         return;
00233     if (!d->ref.deref())
00234         free(d);
00235 }

Here is the call graph for this function:


Member Function Documentation

template<typename T>
QLinkedList< T > & QLinkedList< T >::operator= ( const QLinkedList< T > &  other  ) 

Assigns other to this list and returns a reference to this list.

Definition at line 281 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::detach_helper(), QLinkedList< T >::free(), l, qAtomicSetPtr(), QLinkedListData::sharable, and x.

Referenced by Q3ValueList< bool >::operator=().

00282 {
00283     if (d != l.d) {
00284         QLinkedListData *x = l.d;
00285         x->ref.ref();
00286         x = qAtomicSetPtr(&d, x);
00287         if (!x->ref.deref())
00288             free(x);
00289         if (!d->sharable)
00290             detach_helper();
00291     }
00292     return *this;
00293 }

Here is the call graph for this function:

template<typename T>
bool QLinkedList< T >::operator== ( const QLinkedList< T > &  other  )  const

Returns true if other is equal to this list; otherwise returns false.

Two lists are considered equal if they contain the same values in the same order.

This function requires the value type to implement operator==().

See also:
operator!=()

Definition at line 296 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::e, i, l, QLinkedListNode< T >::n, QLinkedListData::size, and QLinkedListNode< T >::t.

Referenced by Q3ValueList< bool >::operator==().

00297 {
00298     if (d->size != l.d->size)
00299         return false;
00300     if (e == l.e)
00301         return true;
00302     Node *i = e->n;
00303     Node *il = l.e->n;
00304     while (i != e) {
00305         if (! (i->t == il->t))
00306             return false;
00307         i = i->n;
00308         il = il->n;
00309     }
00310     return true;
00311 }

template<class T>
bool QLinkedList< T >::operator!= ( const QLinkedList< T > &  other  )  const [inline]

Returns true if other is not equal to this list; otherwise returns false.

Two lists are considered equal if they contain the same values in the same order.

This function requires the value type to implement operator==().

See also:
operator==()

Definition at line 69 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::operator!=().

00069 { return !(*this == l); }

template<class T>
int QLinkedList< T >::size (  )  const [inline]

Returns the number of items in the list.

See also:
isEmpty(), count()

Definition at line 71 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::at(), and Q3IconDrag::encodedData().

00071 { return d->size; }

template<class T>
void QLinkedList< T >::detach (  )  [inline]

Definition at line 72 of file qlinkedlist.h.

Referenced by QLinkedList< T >::append(), Q3ValueList< bool >::at(), QLinkedList< bool >::begin(), QLinkedList< bool >::end(), QLinkedList< T >::erase(), QLinkedList< T >::operator+=(), Q3ValueList< bool >::operator=(), QLinkedList< T >::prepend(), QLinkedList< bool >::QLinkedList(), QLinkedList< T >::removeAll(), and QLinkedList< bool >::setSharable().

00073     { if (d->ref != 1) detach_helper(); }

template<class T>
bool QLinkedList< T >::isDetached (  )  const [inline]

Definition at line 74 of file qlinkedlist.h.

00074 { return d->ref == 1; }

template<class T>
void QLinkedList< T >::setSharable ( bool  sharable  )  [inline]

Definition at line 75 of file qlinkedlist.h.

00075 { if (!sharable) detach(); d->sharable = sharable; }

template<class T>
bool QLinkedList< T >::isEmpty (  )  const [inline]

Returns true if the list contains no items; otherwise returns false.

See also:
size()

Definition at line 77 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::empty(), QLinkedList< bool >::first(), QLinkedList< bool >::last(), Q3GListIteratorList::remove(), QLinkedList< bool >::removeFirst(), QLinkedList< bool >::removeLast(), and Q3Socket::tryConnecting().

00077 { return d->size == 0; }

template<typename T>
void QLinkedList< T >::clear (  ) 

Removes all the items in the list.

See also:
removeAll()

Definition at line 275 of file qlinkedlist.h.

Referenced by Q3IconView::contentsDragLeaveEvent(), Q3IconDragPrivate::decode(), Q3ValueList< bool >::operator=(), Q3IconView::Q3IconView(), Q3DataTable::reset(), Q3DataTable::setSqlCursor(), and Q3Socket::tryConnecting().

00276 {
00277     *this = QLinkedList<T>();
00278 }

template<typename T>
void QLinkedList< T >::append ( const T &  value  ) 

Inserts value at the end of the list.

Example:

        QLinkedList<QString> list;
        list.append("one");
        list.append("two");
        list.append("three");
        // list: ["one", "two", "three"]

This is the same as list.insert(end(), value).

See also:
operator<<(), prepend(), insert()

Definition at line 314 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::detach(), QLinkedList< T >::e, i, and QLinkedListData::size.

Referenced by Q3ValueList< bool >::append(), Q3IconDrag::append(), Q3IconDragPrivate::decode(), QLinkedList< bool >::operator+=(), QLinkedList< bool >::operator<<(), and QLinkedList< bool >::push_back().

00315 {
00316     detach();
00317     Node *i = new Node(t);
00318     i->n = e;
00319     i->p = e->p;
00320     i->p->n = i;
00321     e->p = i;
00322     d->size++;
00323 }

Here is the call graph for this function:

template<typename T>
void QLinkedList< T >::prepend ( const T &  value  ) 

Inserts value at the beginning of the list.

Example:

        QLinkedList<QString> list;
        list.prepend("one");
        list.prepend("two");
        list.prepend("three");
        // list: ["three", "two", "one"]

This is the same as list.insert(begin(), value).

See also:
append(), insert()

Definition at line 326 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::detach(), QLinkedList< T >::e, i, and QLinkedListData::size.

Referenced by Q3ValueList< bool >::prepend(), and QLinkedList< bool >::push_front().

00327 {
00328     detach();
00329     Node *i = new Node(t);
00330     i->n = e->n;
00331     i->p = e;
00332     i->n->p = i;
00333     e->n = i;
00334     d->size++;
00335 }

Here is the call graph for this function:

template<typename T>
T QLinkedList< T >::takeFirst (  )  [inline]

Removes the first item in the list and returns it.

If you don't use the return value, removeFirst() is more efficient.

See also:
takeLast(), removeFirst()

Definition at line 361 of file qlinkedlist.h.

References QLinkedList< T >::first(), QLinkedList< T >::removeFirst(), and T.

00362 {
00363     T t = first();
00364     removeFirst();
00365     return t;
00366 }

Here is the call graph for this function:

template<typename T>
T QLinkedList< T >::takeLast (  )  [inline]

Removes the last item in the list and returns it.

If you don't use the return value, removeLast() is more efficient.

See also:
takeFirst(), removeLast()

Definition at line 369 of file qlinkedlist.h.

References QLinkedList< T >::last(), QLinkedList< T >::removeLast(), and T.

00370 {
00371     T t = last();
00372     removeLast();
00373     return t;
00374 }

Here is the call graph for this function:

template<typename T>
int QLinkedList< T >::removeAll ( const T &  value  ) 

Removes all occurrences of value in the list.

Example:

        QList<QString> list;
        list << "sun" << "cloud" << "sun" << "rain";
        list.removeAll("sun");
        // list: ["cloud", "rain"]

This function requires the value type to have an implementation of operator==().

See also:
insert()

Definition at line 338 of file qlinkedlist.h.

References c, QLinkedList< T >::d, QLinkedList< T >::detach(), QLinkedList< T >::e, i, n, QLinkedListData::size, and T.

Referenced by Q3ValueList< bool >::remove().

00339 {
00340     detach();
00341     const T t = _t;
00342     Node *i = e->n;
00343     int c = 0;
00344     while (i != e) {
00345         if (i->t == t) {
00346             Node *n = i;
00347             i->n->p = i->p;
00348             i->p->n = i->n;
00349             i = i->n;
00350             delete n;
00351             c++;
00352         } else {
00353             i = i->n;
00354         }
00355     }
00356     d->size-=c;
00357     return c;
00358 }

Here is the call graph for this function:

template<typename T>
bool QLinkedList< T >::contains ( const T &  value  )  const

Returns true if the list contains an occurrence of value; otherwise returns false.

This function requires the value type to have an implementation of operator==().

See also:
QListIterator::findNext(), QListIterator::findPrevious()

Definition at line 377 of file qlinkedlist.h.

References QLinkedList< T >::e, and i.

00378 {
00379     Node *i = e;
00380     while ((i = i->n) != e)
00381         if (i->t == t)
00382             return true;
00383     return false;
00384 }

template<typename T>
int QLinkedList< T >::count ( const T &  value  )  const

Returns the number of occurrences of value in the list.

This function requires the value type to have an implementation of operator==().

See also:
contains()

Definition at line 387 of file qlinkedlist.h.

References c, QLinkedList< T >::e, and i.

Referenced by Q3CanvasPixmapArray::Q3CanvasPixmapArray(), and Q3Socket::tryConnecting().

00388 {
00389     Node *i = e;
00390     int c = 0;
00391     while ((i = i->n) != e)
00392         if (i->t == t)
00393             c++;
00394     return c;
00395 }

template<class T>
QLinkedList::iterator QLinkedList< T >::begin (  )  [inline]

Returns an {STL-style iterator} pointing to the first item in the list.

See also:
constBegin(), end()

Definition at line 155 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::at(), Q3IconDrag::encodedData(), Q3IconView::eventFilter(), QLinkedList< bool >::first(), Q3FileDialog::insertEntry(), Q3GListIteratorList::notifyClear(), Q3GListIteratorList::notifyRemove(), Q3ValueList< bool >::operator==(), Q3ValueList< bool >::prepend(), Q3CanvasPixmapArray::Q3CanvasPixmapArray(), QLinkedList< bool >::removeFirst(), Q3Socket::tryConnecting(), and Q3Canvas::~Q3Canvas().

00155 { detach(); return e->n; }

template<class T>
QLinkedList::const_iterator QLinkedList< T >::begin (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 156 of file qlinkedlist.h.

00156 { return e->n; }

template<class T>
QLinkedList::const_iterator QLinkedList< T >::constBegin (  )  const [inline]

Returns a const {STL-style iterator} pointing to the first item in the list.

See also:
begin(), constEnd()

Definition at line 157 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::toStdList().

00157 { return e->n; }

template<class T>
QLinkedList::iterator QLinkedList< T >::end (  )  [inline]

Returns an {STL-style iterator} pointing to the imaginary item after the last item in the list.

See also:
begin(), constEnd()

Definition at line 158 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::append(), Q3IconDrag::encodedData(), Q3IconView::eventFilter(), Q3ValueList< bool >::fromLast(), Q3FileDialog::insertEntry(), QLinkedList< bool >::last(), Q3GListIteratorList::notifyClear(), Q3GListIteratorList::notifyRemove(), Q3ValueList< bool >::operator==(), QLinkedList< bool >::removeLast(), Q3DataTable::setColumnWidth(), and Q3Canvas::~Q3Canvas().

00158 { detach(); return e; }

template<class T>
QLinkedList::const_iterator QLinkedList< T >::end (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 159 of file qlinkedlist.h.

00159 { return e; }

template<class T>
QLinkedList::const_iterator QLinkedList< T >::constEnd (  )  const [inline]

Returns a const {STL-style iterator} pointing to the imaginary item after the last item in the list.

See also:
constBegin(), end()

Definition at line 160 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::operator QList(), and QLinkedList< bool >::toStdList().

00160 { return e; }

template<typename T>
QLinkedList< T >::iterator QLinkedList< T >::insert ( iterator  before,
const T &  value 
)

Inserts value in front of the item pointed to by the iterator before. Returns an iterator pointing at the inserted item.

See also:
erase()

Definition at line 399 of file qlinkedlist.h.

References QLinkedList< T >::d, i, QLinkedList< T >::iterator::i, m, and QLinkedListData::size.

Referenced by Q3ValueList< bool >::insert().

00400 {
00401     Node *i = before.i;
00402     Node *m = new Node(t);
00403     m->n = i;
00404     m->p = i->p;
00405     m->p->n = m;
00406     i->p = m;
00407     d->size++;
00408     return m;
00409 }

template<typename T>
QLinkedList< T >::iterator QLinkedList< T >::erase ( iterator  pos  ) 

Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next item in the list (which may be end()).

See also:
insert()

Definition at line 422 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::detach(), QLinkedList< T >::e, i, QLinkedList< T >::iterator::i, n, and QLinkedListData::size.

Referenced by Q3ValueList< bool >::remove(), QLinkedList< bool >::removeFirst(), and QLinkedList< bool >::removeLast().

00423 {
00424     detach();
00425     Node *i = pos.i;
00426     if (i != e) {
00427         Node *n = i;
00428         i->n->p = i->p;
00429         i->p->n = i->n;
00430         i = i->n;
00431         delete n;
00432         d->size--;
00433     }
00434     return i;
00435 }

Here is the call graph for this function:

template<class T>
QLinkedList::iterator QLinkedList< T >::erase ( iterator  begin,
iterator  end 
)

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Removes all the items from begin up to (but not including) end.

template<class T>
int QLinkedList< T >::count (  )  const [inline]

Same as size().

Definition at line 168 of file qlinkedlist.h.

Referenced by Q3ValueList< bool >::contains().

00168 { return d->size; }

template<class T>
T & QLinkedList< T >::first (  )  [inline]

Returns a reference to the first item in the list. This function assumes that the list isn't empty.

See also:
last(), isEmpty()

Definition at line 169 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::front(), and QLinkedList< T >::takeFirst().

00169 { Q_ASSERT(!isEmpty()); return *begin(); }

template<class T>
const T & QLinkedList< T >::first (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 170 of file qlinkedlist.h.

00170 { Q_ASSERT(!isEmpty()); return *begin(); }

template<class T>
T & QLinkedList< T >::last (  )  [inline]

Returns a reference to the last item in the list. This function assumes that the list isn't empty.

See also:
first(), isEmpty()

Definition at line 171 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::back(), and QLinkedList< T >::takeLast().

00171 { Q_ASSERT(!isEmpty()); return *(--end()); }

template<class T>
const T & QLinkedList< T >::last (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 172 of file qlinkedlist.h.

00172 { Q_ASSERT(!isEmpty()); return *(--end()); }

template<class T>
void QLinkedList< T >::removeFirst (  )  [inline]

Removes the first item in the list.

This is the same as erase(begin()).

See also:
removeLast(), erase()

Definition at line 173 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::pop_front(), and QLinkedList< T >::takeFirst().

00173 { Q_ASSERT(!isEmpty()); erase(begin()); }

template<class T>
void QLinkedList< T >::removeLast (  )  [inline]

Removes the last item in the list.

See also:
removeFirst(), erase()

Definition at line 174 of file qlinkedlist.h.

Referenced by QLinkedList< bool >::pop_back(), and QLinkedList< T >::takeLast().

00174 { Q_ASSERT(!isEmpty()); erase(--end()); }

template<class T>
void QLinkedList< T >::push_back ( const T &  value  )  [inline]

This function is provided for STL compatibility. It is equivalent to append(value).

Definition at line 177 of file qlinkedlist.h.

00177 { append(t); }

template<class T>
void QLinkedList< T >::push_front ( const T &  value  )  [inline]

This function is provided for STL compatibility. It is equivalent to prepend(value).

Definition at line 178 of file qlinkedlist.h.

Referenced by Q3GListIteratorList::add().

00178 { prepend(t); }

template<class T>
T & QLinkedList< T >::front (  )  [inline]

This function is provided for STL compatibility. It is equivalent to first().

Definition at line 179 of file qlinkedlist.h.

00179 { return first(); }

template<class T>
const T & QLinkedList< T >::front (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 180 of file qlinkedlist.h.

00180 { return first(); }

template<class T>
T & QLinkedList< T >::back (  )  [inline]

This function is provided for STL compatibility. It is equivalent to last().

Definition at line 181 of file qlinkedlist.h.

00181 { return last(); }

template<class T>
const T & QLinkedList< T >::back (  )  const [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Definition at line 182 of file qlinkedlist.h.

00182 { return last(); }

template<class T>
void QLinkedList< T >::pop_front (  )  [inline]

This function is provided for STL compatibility. It is equivalent to removeFirst().

Definition at line 183 of file qlinkedlist.h.

00183 { removeFirst(); }

template<class T>
void QLinkedList< T >::pop_back (  )  [inline]

This function is provided for STL compatibility. It is equivalent to removeLast().

Definition at line 184 of file qlinkedlist.h.

00184 { removeLast(); }

template<class T>
bool QLinkedList< T >::empty (  )  const [inline]

This function is provided for STL compatibility. It is equivalent to isEmpty().

Definition at line 185 of file qlinkedlist.h.

00185 { return isEmpty(); }

template<class T>
QLinkedList< T > QLinkedList< T >::fromStdList ( const std::list< T > &  list  )  [inline, static]

Since:
4.1
Returns a QLinkedList object with the data contained in list. The order of the elements in the QLinkedList is the same as in list.

Example:

        std::list<double> stdlist;
        list.push_back(1.2);
        list.push_back(0.5);
        list.push_back(3.14);

        QLinkedList<double> list = QLinkedList<double>::fromStdList(stdlist);

See also:
toStdList()

Definition at line 195 of file qlinkedlist.h.

00196     { QLinkedList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }

template<class T>
std::list< T > QLinkedList< T >::toStdList (  )  const [inline]

Since:
4.1
Returns a std::list object with the data contained in this QLinkedList. Example:

        QLinkedList<double> list;
        list << 1.2 << 0.5 << 3.14;

        std::list<double> stdlist = list.toStdList();

See also:
fromStdList()

Definition at line 197 of file qlinkedlist.h.

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

template<typename T>
QLinkedList< T > & QLinkedList< T >::operator+= ( const QLinkedList< T > &  other  ) 

Appends the items of the other list to this list and returns a reference to this list.

See also:
operator+(), append()

Definition at line 438 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::detach(), QLinkedList< T >::e, i, l, n, o, and QLinkedListData::size.

Referenced by Q3ValueList< bool >::operator+=().

00439 {
00440     detach();
00441     int n = l.d->size;
00442     d->size += n;
00443     Node *o = l.e->n;
00444     while (n--) {
00445         Node *i = new Node(o->t);
00446         o = o->n;
00447         i->n = e;
00448         i->p = e->p;
00449         i->p->n = i;
00450         e->p = i;
00451     }
00452     return *this;
00453 }

Here is the call graph for this function:

template<typename T>
QLinkedList< T > QLinkedList< T >::operator+ ( const QLinkedList< T > &  other  )  const

Returns a list that contains all the items in this list followed by all the items in the other list.

See also:
operator+=()

Definition at line 456 of file qlinkedlist.h.

References l, and n.

Referenced by Q3ValueList< bool >::operator+().

00457 {
00458     QLinkedList<T> n = *this;
00459     n += l;
00460     return n;
00461 }

template<class T>
void QLinkedList< T >::operator+= ( const T &  value  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Appends value to the list.

Definition at line 219 of file qlinkedlist.h.

00219 { append(t); return *this; }

template<class T>
QLinkedList< T > & QLinkedList< T >::operator<< ( const T &  value  )  [inline]

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Appends value to the list.

Definition at line 220 of file qlinkedlist.h.

00220 { append(t); return *this; }

template<class T>
QLinkedList< T > & QLinkedList< T >::operator<< ( const QLinkedList< T > &  other  )  [inline]

Appends the items of the other list to this list and returns a reference to this list.

See also:
operator+=(), append()

Definition at line 221 of file qlinkedlist.h.

00221 { *this += l; return *this; }

template<typename T>
void QLinkedList< T >::detach_helper (  )  [private]

Definition at line 238 of file qlinkedlist.h.

References QLinkedList< T >::d, QLinkedList< T >::e, QLinkedList< T >::free(), i, j, qAtomicSetPtr(), QLinkedListData::size, and x.

Referenced by QLinkedList< bool >::detach(), and QLinkedList< T >::operator=().

00239 {
00240     union { QLinkedListData *d; Node *e; } x;
00241     x.d = new QLinkedListData;
00242     x.d->ref.init(1);
00243     x.d->size = d->size;
00244     x.d->sharable = true;
00245     Node *i = e->n, *j = x.e;
00246     while (i != e) {
00247         j->n = new Node(i->t);
00248         j->n->p = j;
00249         i = i->n;
00250         j = j->n;
00251     }
00252     j->n = x.e;
00253     x.e->p = j;
00254     x.d = qAtomicSetPtr(&d, x.d);
00255     if (!x.d->ref.deref())
00256         free(x.d);
00257 }

Here is the call graph for this function:

template<typename T>
void QLinkedList< T >::free ( QLinkedListData  )  [private]

Definition at line 260 of file qlinkedlist.h.

References n, x, and y.

Referenced by QLinkedList< T >::detach_helper(), QLinkedList< T >::operator=(), and QLinkedList< T >::~QLinkedList().

00261 {
00262     Node *y = reinterpret_cast<Node*>(x);
00263     Node *i = y->n;
00264     if (x->ref == 0) {
00265         while(i != y) {
00266             Node *n = i;
00267             i = i->n;
00268             delete n;
00269         }
00270         delete x;
00271     }
00272 }


Friends And Related Function Documentation

template<class T>
friend class iterator [friend]

Definition at line 122 of file qlinkedlist.h.

template<class T>
friend class const_iterator [friend]

Definition at line 152 of file qlinkedlist.h.

template<class T>
QDataStream & operator<< ( QDataStream out,
const QLinkedList< T > &  list 
) [related]

Writes the linked list list to stream out.

This function requires the value type to implement operator<<().

See also:
Format of the QDataStream operators

Definition at line 250 of file qdatastream.h.

00251 {
00252     s << quint32(l.size());
00253     typename QLinkedList<T>::ConstIterator it = l.constBegin();
00254     for(; it != l.constEnd(); ++it)
00255         s << *it;
00256     return s;
00257 }

template<class T>
QDataStream & operator>> ( QDataStream in,
QLinkedList< T > &  list 
) [related]

Reads a linked list from stream in into list.

This function requires the value type to implement operator>>().

See also:
Format of the QDataStream operators

Definition at line 233 of file qdatastream.h.

00234 {
00235     l.clear();
00236     quint32 c;
00237     s >> c;
00238     for(quint32 i = 0; i < c; ++i)
00239     {
00240         T t;
00241         s >> t;
00242         l.append(t);
00243         if (s.atEnd())
00244             break;
00245     }
00246     return s;
00247 }


Member Data Documentation

template<class T>
QLinkedListData* QLinkedList< T >::d [private]

Definition at line 61 of file qlinkedlist.h.

Referenced by QLinkedList< T >::append(), QLinkedList< bool >::count(), QLinkedList< bool >::detach(), QLinkedList< T >::detach_helper(), QLinkedList< T >::erase(), QLinkedList< T >::insert(), QLinkedList< bool >::isDetached(), QLinkedList< bool >::isEmpty(), QLinkedList< T >::operator+=(), QLinkedList< T >::operator=(), QLinkedList< T >::operator==(), QLinkedList< T >::prepend(), QLinkedList< bool >::QLinkedList(), QLinkedList< T >::removeAll(), QLinkedList< bool >::setSharable(), QLinkedList< bool >::size(), and QLinkedList< T >::~QLinkedList().

template<class T>
QLinkedListNode<T>* QLinkedList< T >::e [private]

Definition at line 61 of file qlinkedlist.h.

Referenced by QLinkedList< T >::append(), QLinkedList< bool >::begin(), QLinkedList< bool >::constBegin(), QLinkedList< bool >::constEnd(), QLinkedList< T >::contains(), QLinkedList< T >::count(), QLinkedList< T >::detach_helper(), QLinkedList< bool >::end(), QLinkedList< T >::erase(), QLinkedList< T >::operator+=(), QLinkedList< T >::operator==(), QLinkedList< T >::prepend(), and QLinkedList< T >::removeAll().

union { ... } [private]


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 18:12:59 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1