Qt 4.8
Classes | Public Types | Public Functions | Static Public Functions | Private Types | Private Functions | Properties | Friends | Related Functions | List of all members
QLinkedList< T > Class Template Reference

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

#include <qdatastream.h>

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...
 

Public Types

typedef const value_typeconst_pointer
 Typedef for const T *. More...
 
typedef const value_typeconst_reference
 Typedef for const T &. More...
 
typedef const_iterator ConstIterator
 Qt-style synonym for QLinkedList::const_iterator. More...
 
typedef qptrdiff difference_type
 Typedef for ptrdiff_t. More...
 
typedef iterator Iterator
 Qt-style synonym for QLinkedList::iterator. More...
 
typedef value_typepointer
 Typedef for T *. More...
 
typedef value_typereference
 Typedef for T &. More...
 
typedef int size_type
 Typedef for int. More...
 
typedef T value_type
 Typedef for T. More...
 

Public Functions

void append (const T &)
 Inserts value at the end of the list. More...
 
T & back ()
 This function is provided for STL compatibility. More...
 
const T & back () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator begin ()
 Returns an STL-style iterator pointing to the first item in the list. More...
 
const_iterator begin () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
void clear ()
 Removes all the items in the list. More...
 
const_iterator constBegin () const
 Returns a const STL-style iterator pointing to the first item in the list. More...
 
const_iterator constEnd () const
 Returns a const STL-style iterator pointing to the imaginary item after the last item in the list. More...
 
bool contains (const T &t) const
 Returns true if the list contains an occurrence of value; otherwise returns false. More...
 
int count (const T &t) const
 Returns the number of occurrences of value in the list. More...
 
int count () const
 Same as size(). More...
 
void detach ()
 
bool empty () const
 This function is provided for STL compatibility. More...
 
iterator end ()
 Returns an STL-style iterator pointing to the imaginary item after the last item in the list. More...
 
const_iterator end () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool endsWith (const T &t) const
 Returns true if the list is not empty and its last item is equal to value; otherwise returns false. More...
 
iterator 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()). More...
 
iterator erase (iterator first, iterator last)
 Removes all the items from begin up to (but not including) end. More...
 
T & first ()
 Returns a reference to the first item in the list. More...
 
const T & first () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
T & front ()
 This function is provided for STL compatibility. More...
 
const T & front () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
iterator insert (iterator before, const T &t)
 Inserts value in front of the item pointed to by the iterator before. More...
 
bool isDetached () const
 
bool isEmpty () const
 Returns true if the list contains no items; otherwise returns false. More...
 
bool isSharedWith (const QLinkedList< T > &other) const
 
T & last ()
 Returns a reference to the last item in the list. More...
 
const T & last () const
 This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More...
 
bool operator!= (const QLinkedList< T > &l) const
 Returns true if other is not equal to this list; otherwise returns false. More...
 
QLinkedList< T > operator+ (const QLinkedList< T > &l) const
 Returns a list that contains all the items in this list followed by all the items in the other list. More...
 
QLinkedList< T > & operator+= (const QLinkedList< T > &l)
 Appends the items of the other list to this list and returns a reference to this list. More...
 
QLinkedList< T > & operator+= (const T &t)
 Appends value to the list. More...
 
QLinkedList< T > & operator<< (const QLinkedList< T > &l)
 Appends the items of the other list to this list and returns a reference to this list. More...
 
QLinkedList< T > & operator<< (const T &t)
 Appends value to the list. More...
 
QLinkedList< T > & operator= (const QLinkedList< T > &)
 Assigns other to this list and returns a reference to this list. More...
 
bool operator== (const QLinkedList< T > &l) const
 Returns true if other is equal to this list; otherwise returns false. More...
 
void pop_back ()
 This function is provided for STL compatibility. More...
 
void pop_front ()
 This function is provided for STL compatibility. More...
 
void prepend (const T &)
 Inserts value at the beginning of the list. More...
 
void push_back (const T &t)
 This function is provided for STL compatibility. More...
 
void push_front (const T &t)
 This function is provided for STL compatibility. More...
 
 QLinkedList ()
 Constructs an empty list. More...
 
 QLinkedList (const QLinkedList< T > &l)
 Constructs a copy of other. More...
 
int removeAll (const T &t)
 Removes all occurrences of value in the list. More...
 
void removeFirst ()
 Removes the first item in the list. More...
 
void removeLast ()
 Removes the last item in the list. More...
 
bool removeOne (const T &t)
 Removes the first occurrences of value in the list. More...
 
void setSharable (bool sharable)
 
int size () const
 Returns the number of items in the list. More...
 
bool startsWith (const T &t) const
 Returns true if the list is not empty and its first item is equal to value; otherwise returns false. More...
 
void swap (QLinkedList< T > &other)
 Swaps list other with this list. More...
 
takeFirst ()
 Removes the first item in the list and returns it. More...
 
takeLast ()
 Removes the last item in the list and returns it. More...
 
std::list< T > toStdList () const
 Returns a std::list object with the data contained in this QLinkedList. More...
 
 ~QLinkedList ()
 Destroys the list. More...
 

Static Public Functions

static QLinkedList< T > fromStdList (const std::list< T > &list)
 Returns a QLinkedList object with the data contained in list. More...
 

Private Types

typedef QLinkedListNode< T > Node
 

Private Functions

void detach_helper ()
 
void free (QLinkedListData *)
 

Properties

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

Friends

class const_iterator
 
class iterator
 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QLinkedList< T > &list)
 Writes the linked list list to stream out. More...
 
QDataStreamoperator>> (QDataStream &in, QLinkedList< T > &list)
 Reads a linked list from stream in into list. More...
 

Detailed Description

template<class T>
class QLinkedList< T >

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

Note
This class or function is reentrant.

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:

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

QLinkedList<int> integerList;

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

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:

...
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
QLinkedListIterator, QMutableLinkedListIterator, QList, QVector

Definition at line 63 of file qdatastream.h.

Typedefs

◆ const_pointer

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

Typedef for const T *.

Provided for STL compatibility.

Definition at line 218 of file qlinkedlist.h.

◆ const_reference

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

Typedef for const T &.

Provided for STL compatibility.

Definition at line 220 of file qlinkedlist.h.

◆ ConstIterator

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

Qt-style synonym for QLinkedList::const_iterator.

Definition at line 194 of file qlinkedlist.h.

◆ difference_type

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

Typedef for ptrdiff_t.

Provided for STL compatibility.

Definition at line 221 of file qlinkedlist.h.

◆ Iterator

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

Qt-style synonym for QLinkedList::iterator.

Definition at line 193 of file qlinkedlist.h.

◆ Node

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

Definition at line 80 of file qlinkedlist.h.

◆ pointer

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

Typedef for T *.

Provided for STL compatibility.

Definition at line 217 of file qlinkedlist.h.

◆ reference

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

Typedef for T &.

Provided for STL compatibility.

Definition at line 219 of file qlinkedlist.h.

◆ size_type

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

Typedef for int.

Provided for STL compatibility.

Definition at line 215 of file qlinkedlist.h.

◆ value_type

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

Typedef for T.

Provided for STL compatibility.

Definition at line 216 of file qlinkedlist.h.

Constructors and Destructors

◆ QLinkedList() [1/2]

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

Constructs an empty list.

Definition at line 84 of file qlinkedlist.h.

QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
static QLinkedListData shared_null
Definition: qlinkedlist.h:66

◆ QLinkedList() [2/2]

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 85 of file qlinkedlist.h.

85 : d(l.d) { d->ref.ref(); if (!d->sharable) detach(); }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
void detach()
Definition: qlinkedlist.h:97
QFactoryLoader * l

◆ ~QLinkedList()

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 258 of file qlinkedlist.h.

259 {
260  if (!d)
261  return;
262  if (!d->ref.deref())
263  free(d);
264 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
void free(QLinkedListData *)
Definition: qlinkedlist.h:296

Functions

◆ append()

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

Inserts value at the end of the list.

Example:

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 350 of file qlinkedlist.h.

Referenced by QSpanCollection::addSpan(), QScriptValueIteratorPrivate::ensureInitialized(), operator>>(), QSpanCollection::updateRemovedColumns(), and QSpanCollection::updateRemovedRows().

351 {
352  detach();
353  Node *i = new Node(t);
354  i->n = e;
355  i->p = e->p;
356  i->p->n = i;
357  e->p = i;
358  d->size++;
359 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97
QLinkedListNode< T > Node
Definition: qlinkedlist.h:80

◆ back() [1/2]

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

This function is provided for STL compatibility.

It is equivalent to last().

Definition at line 210 of file qlinkedlist.h.

210 { return last(); }
T & last()
Returns a reference to the last item in the list.
Definition: qlinkedlist.h:198

◆ back() [2/2]

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 211 of file qlinkedlist.h.

211 { return last(); }
T & last()
Returns a reference to the last item in the list.
Definition: qlinkedlist.h:198

◆ begin() [1/2]

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

◆ begin() [2/2]

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 183 of file qlinkedlist.h.

183 { return e->n; }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ clear()

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

Removes all the items in the list.

See also
removeAll()

Definition at line 311 of file qlinkedlist.h.

Referenced by QSpanCollection::clear(), operator>>(), and QScriptValueIteratorPrivate::~QScriptValueIteratorPrivate().

312 {
313  *this = QLinkedList<T>();
314 }
The QLinkedList class is a template class that provides linked lists.
Definition: qdatastream.h:63

◆ constBegin()

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 184 of file qlinkedlist.h.

184 { return e->n; }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ constEnd()

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 187 of file qlinkedlist.h.

187 { return e; }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ contains()

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
QLinkedListIterator::findNext(), QLinkedListIterator::findPrevious()

Definition at line 425 of file qlinkedlist.h.

Referenced by QSpanCollection::updateRemovedColumns().

426 {
427  Node *i = e;
428  while ((i = i->n) != e)
429  if (i->t == t)
430  return true;
431  return false;
432 }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ count() [1/2]

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 435 of file qlinkedlist.h.

436 {
437  Node *i = e;
438  int c = 0;
439  while ((i = i->n) != e)
440  if (i->t == t)
441  c++;
442  return c;
443 }
unsigned char c[8]
Definition: qnumeric_p.h:62
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ count() [2/2]

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

Same as size().

Definition at line 195 of file qlinkedlist.h.

195 { return d->size; }
QLinkedListData * d
Definition: qlinkedlist.h:81

◆ detach()

template<class T>
void QLinkedList< T >::detach ( )
inline
Warning
This function is not part of the public interface.

Definition at line 97 of file qlinkedlist.h.

98  { if (d->ref != 1) detach_helper(); }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
void detach_helper()
Definition: qlinkedlist.h:267

◆ detach_helper()

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

Definition at line 267 of file qlinkedlist.h.

268 {
269  union { QLinkedListData *d; Node *e; } x;
270  x.d = new QLinkedListData;
271  x.d->ref = 1;
272  x.d->size = d->size;
273  x.d->sharable = true;
274  Node *original = e->n;
275  Node *copy = x.e;
276  while (original != e) {
277  QT_TRY {
278  copy->n = new Node(original->t);
279  copy->n->p = copy;
280  original = original->n;
281  copy = copy->n;
282  } QT_CATCH(...) {
283  copy->n = x.e;
284  free(x.d);
285  QT_RETHROW;
286  }
287  }
288  copy->n = x.e;
289  x.e->p = copy;
290  if (!d->ref.deref())
291  free(d);
292  d = x.d;
293 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
#define QT_RETHROW
Definition: qglobal.h:1539
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
#define QT_CATCH(A)
Definition: qglobal.h:1537
QLinkedListNode< T > Node
Definition: qlinkedlist.h:80
void free(QLinkedListData *)
Definition: qlinkedlist.h:296
#define QT_TRY
Definition: qglobal.h:1536

◆ empty()

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

This function is provided for STL compatibility.

It is equivalent to isEmpty() and returns true if the list is empty.

Definition at line 214 of file qlinkedlist.h.

214 { return isEmpty(); }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103

◆ end() [1/2]

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 185 of file qlinkedlist.h.

Referenced by QGraphicsAnchorLayoutPrivate::getGraphParts(), QSpanCollection::updateInsertedColumns(), QSpanCollection::updateInsertedRows(), QSpanCollection::updateRemovedColumns(), and QSpanCollection::updateRemovedRows().

185 { detach(); return e; }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97

◆ end() [2/2]

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 186 of file qlinkedlist.h.

186 { return e; }
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81

◆ endsWith()

template<class T>
bool QLinkedList< T >::endsWith ( const T &  value) const
inline

Returns true if the list is not empty and its last item is equal to value; otherwise returns false.

Since
4.5
See also
isEmpty(), last()

Definition at line 203 of file qlinkedlist.h.

203 { return !isEmpty() && last() == t; }
T & last()
Returns a reference to the last item in the list.
Definition: qlinkedlist.h:198
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103

◆ erase() [1/2]

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 470 of file qlinkedlist.h.

Referenced by QGraphicsAnchorLayoutPrivate::getGraphParts(), QLinkedList< JSC::Identifier >::insert(), QSpanCollection::updateRemovedColumns(), QSpanCollection::updateRemovedRows(), and QMeeGoLivePixmapData::~QMeeGoLivePixmapData().

471 {
472  detach();
473  Node *i = pos.i;
474  if (i != e) {
475  Node *n = i;
476  i->n->p = i->p;
477  i->p->n = i->n;
478  i = i->n;
479  delete n;
480  d->size--;
481  }
482  return i;
483 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97

◆ erase() [2/2]

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

Removes all the items from begin up to (but not including) end.

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

◆ first() [1/2]

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 196 of file qlinkedlist.h.

196 { Q_ASSERT(!isEmpty()); return *begin(); }
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ first() [2/2]

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 197 of file qlinkedlist.h.

197 { Q_ASSERT(!isEmpty()); return *begin(); }
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ free()

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

Definition at line 296 of file qlinkedlist.h.

297 {
298  Node *y = reinterpret_cast<Node*>(x);
299  Node *i = y->n;
300  if (x->ref == 0) {
301  while(i != y) {
302  Node *n = i;
303  i = i->n;
304  delete n;
305  }
306  delete x;
307  }
308 }
QBasicAtomicInt ref
Definition: qlinkedlist.h:62

◆ fromStdList()

template<class T>
QLinkedList< T > QLinkedList< T >::fromStdList ( const std::list< T > &  list)
inlinestatic

Returns a QLinkedList object with the data contained in list.

Since
4.1

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);
See also
toStdList()

Definition at line 224 of file qlinkedlist.h.

225  { QLinkedList<T> tmp; qCopy(list.begin(), list.end(), std::back_inserter(tmp)); return tmp; }
The QLinkedList class is a template class that provides linked lists.
Definition: qdatastream.h:63
OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
Definition: qalgorithms.h:79

◆ front() [1/2]

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

This function is provided for STL compatibility.

It is equivalent to first().

Definition at line 208 of file qlinkedlist.h.

208 { return first(); }
T & first()
Returns a reference to the first item in the list.
Definition: qlinkedlist.h:196

◆ front() [2/2]

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 209 of file qlinkedlist.h.

209 { return first(); }
T & first()
Returns a reference to the first item in the list.
Definition: qlinkedlist.h:196

◆ insert()

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 447 of file qlinkedlist.h.

Referenced by QMeeGoLivePixmapData::QMeeGoLivePixmapData().

448 {
449  Node *i = before.i;
450  Node *m = new Node(t);
451  m->n = i;
452  m->p = i->p;
453  m->p->n = m;
454  i->p = m;
455  d->size++;
456  return m;
457 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QLinkedListNode< T > Node
Definition: qlinkedlist.h:80

◆ isDetached()

template<class T>
bool QLinkedList< T >::isDetached ( ) const
inline
Warning
This function is not part of the public interface.

Definition at line 99 of file qlinkedlist.h.

99 { return d->ref == 1; }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62

◆ isEmpty()

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 103 of file qlinkedlist.h.

Referenced by QGraphicsAnchorLayoutPrivate::getGraphParts(), QTableViewPrivate::hasSpans(), QSpanCollection::updateInsertedColumns(), QSpanCollection::updateInsertedRows(), QSpanCollection::updateRemovedColumns(), and QSpanCollection::updateRemovedRows().

103 { return d->size == 0; }
QLinkedListData * d
Definition: qlinkedlist.h:81

◆ isSharedWith()

template<class T>
bool QLinkedList< T >::isSharedWith ( const QLinkedList< T > &  other) const
inline
Warning
This function is not part of the public interface.

Definition at line 101 of file qlinkedlist.h.

101 { return d == other.d; }
QLinkedListData * d
Definition: qlinkedlist.h:81

◆ last() [1/2]

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 198 of file qlinkedlist.h.

198 { Q_ASSERT(!isEmpty()); return *(--end()); }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlinkedlist.h:185

◆ last() [2/2]

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 199 of file qlinkedlist.h.

199 { Q_ASSERT(!isEmpty()); return *(--end()); }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlinkedlist.h:185

◆ operator!=()

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 94 of file qlinkedlist.h.

94 { return !(*this == l); }
QFactoryLoader * l

◆ operator+()

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 511 of file qlinkedlist.h.

512 {
513  QLinkedList<T> n = *this;
514  n += l;
515  return n;
516 }
The QLinkedList class is a template class that provides linked lists.
Definition: qdatastream.h:63
QFactoryLoader * l

◆ operator+=() [1/2]

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 486 of file qlinkedlist.h.

487 {
488  detach();
489  int n = l.d->size;
490  d->size += n;
491  Node *original = l.e->n;
492  while (n--) {
493  QT_TRY {
494  Node *copy = new Node(original->t);
495  original = original->n;
496  copy->n = e;
497  copy->p = e->p;
498  copy->p->n = copy;
499  e->p = copy;
500  } QT_CATCH(...) {
501  // restore the original list
502  while (n++<d->size)
503  removeLast();
504  QT_RETHROW;
505  }
506  }
507  return *this;
508 }
QLinkedListData * d
Definition: qlinkedlist.h:81
#define QT_RETHROW
Definition: qglobal.h:1539
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
#define QT_CATCH(A)
Definition: qglobal.h:1537
void detach()
Definition: qlinkedlist.h:97
QLinkedListNode< T > Node
Definition: qlinkedlist.h:80
void removeLast()
Removes the last item in the list.
Definition: qlinkedlist.h:201
QFactoryLoader * l
#define QT_TRY
Definition: qglobal.h:1536

◆ operator+=() [2/2]

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

Appends value to the list.

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 248 of file qlinkedlist.h.

248 { append(t); return *this; }
void append(const T &)
Inserts value at the end of the list.
Definition: qlinkedlist.h:350

◆ operator<<() [1/2]

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

Appends value to the list.

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 249 of file qlinkedlist.h.

249 { append(t); return *this; }
void append(const T &)
Inserts value at the end of the list.
Definition: qlinkedlist.h:350

◆ operator<<() [2/2]

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 250 of file qlinkedlist.h.

250 { *this += l; return *this; }
QFactoryLoader * l

◆ operator=()

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 317 of file qlinkedlist.h.

318 {
319  if (d != l.d) {
320  QLinkedListData *o = l.d;
321  o->ref.ref();
322  if (!d->ref.deref())
323  free(d);
324  d = o;
325  if (!d->sharable)
326  detach_helper();
327  }
328  return *this;
329 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QBasicAtomicInt ref
Definition: qlinkedlist.h:62
void detach_helper()
Definition: qlinkedlist.h:267
QFactoryLoader * l
void free(QLinkedListData *)
Definition: qlinkedlist.h:296

◆ operator==()

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 332 of file qlinkedlist.h.

333 {
334  if (d->size != l.d->size)
335  return false;
336  if (e == l.e)
337  return true;
338  Node *i = e->n;
339  Node *il = l.e->n;
340  while (i != e) {
341  if (! (i->t == il->t))
342  return false;
343  i = i->n;
344  il = il->n;
345  }
346  return true;
347 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
QFactoryLoader * l

◆ pop_back()

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

This function is provided for STL compatibility.

It is equivalent to removeLast().

Definition at line 213 of file qlinkedlist.h.

213 { removeLast(); }
void removeLast()
Removes the last item in the list.
Definition: qlinkedlist.h:201

◆ pop_front()

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

This function is provided for STL compatibility.

It is equivalent to removeFirst().

Definition at line 212 of file qlinkedlist.h.

212 { removeFirst(); }
void removeFirst()
Removes the first item in the list.
Definition: qlinkedlist.h:200

◆ prepend()

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

Inserts value at the beginning of the list.

Example:

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 362 of file qlinkedlist.h.

363 {
364  detach();
365  Node *i = new Node(t);
366  i->n = e->n;
367  i->p = e;
368  i->n->p = i;
369  e->n = i;
370  d->size++;
371 }
QLinkedListData * d
Definition: qlinkedlist.h:81
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97
QLinkedListNode< T > Node
Definition: qlinkedlist.h:80

◆ push_back()

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 206 of file qlinkedlist.h.

206 { append(t); }
void append(const T &)
Inserts value at the end of the list.
Definition: qlinkedlist.h:350

◆ push_front()

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 207 of file qlinkedlist.h.

207 { prepend(t); }
void prepend(const T &)
Inserts value at the beginning of the list.
Definition: qlinkedlist.h:362

◆ removeAll()

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

Removes all occurrences of value in the list.

Example:

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 374 of file qlinkedlist.h.

375 {
376  detach();
377  const T t = _t;
378  Node *i = e->n;
379  int c = 0;
380  while (i != e) {
381  if (i->t == t) {
382  Node *n = i;
383  i->n->p = i->p;
384  i->p->n = i->n;
385  i = i->n;
386  delete n;
387  c++;
388  } else {
389  i = i->n;
390  }
391  }
392  d->size-=c;
393  return c;
394 }
QLinkedListData * d
Definition: qlinkedlist.h:81
unsigned char c[8]
Definition: qnumeric_p.h:62
QLinkedListNode< T > * e
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97

◆ removeFirst()

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 200 of file qlinkedlist.h.

200 { Q_ASSERT(!isEmpty()); erase(begin()); }
iterator erase(iterator pos)
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next it...
Definition: qlinkedlist.h:470
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823

◆ removeLast()

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

Removes the last item in the list.

See also
removeFirst(), erase()

Definition at line 201 of file qlinkedlist.h.

201 { Q_ASSERT(!isEmpty()); erase(--end()); }
iterator erase(iterator pos)
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next it...
Definition: qlinkedlist.h:470
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlinkedlist.h:185

◆ removeOne()

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

Removes the first occurrences of value in the list.

Since
4.4

Returns true on success; otherwise returns false.

Example:

list << "sun" << "cloud" << "sun" << "rain";
list.removeOne("sun");
// list: ["cloud", "sun", "rain"]

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

See also
insert()

Definition at line 397 of file qlinkedlist.h.

Referenced by QSpanCollection::updateSpan().

398 {
399  detach();
400  iterator it = qFind(begin(), end(), _t);
401  if (it != end()) {
402  erase(it);
403  return true;
404  }
405  return false;
406 }
iterator erase(iterator pos)
Removes the item pointed to by the iterator pos from the list, and returns an iterator to the next it...
Definition: qlinkedlist.h:470
#define it(className, varName)
InputIterator qFind(InputIterator first, InputIterator last, const T &val)
Definition: qalgorithms.h:117
iterator begin()
Returns an STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:182
friend class iterator
Definition: qlinkedlist.h:149
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlinkedlist.h:185
void detach()
Definition: qlinkedlist.h:97

◆ setSharable()

template<class T>
void QLinkedList< T >::setSharable ( bool  sharable)
inline
Warning
This function is not part of the public interface.

Definition at line 100 of file qlinkedlist.h.

100 { if (!sharable) detach(); d->sharable = sharable; }
QLinkedListData * d
Definition: qlinkedlist.h:81
void detach()
Definition: qlinkedlist.h:97

◆ size()

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

Returns the number of items in the list.

See also
isEmpty(), count()

Definition at line 96 of file qlinkedlist.h.

96 { return d->size; }
QLinkedListData * d
Definition: qlinkedlist.h:81

◆ startsWith()

template<class T>
bool QLinkedList< T >::startsWith ( const T &  value) const
inline

Returns true if the list is not empty and its first item is equal to value; otherwise returns false.

Since
4.5
See also
isEmpty(), first()

Definition at line 202 of file qlinkedlist.h.

202 { return !isEmpty() && first() == t; }
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlinkedlist.h:103
T & first()
Returns a reference to the first item in the list.
Definition: qlinkedlist.h:196

◆ swap()

template<class T>
void QLinkedList< T >::swap ( QLinkedList< T > &  other)
inline

Swaps list other with this list.

Since
4.8

This operation is very fast and never fails.

Definition at line 92 of file qlinkedlist.h.

92 { qSwap(d, other.d); }
QLinkedListData * d
Definition: qlinkedlist.h:81
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181

◆ takeFirst()

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 409 of file qlinkedlist.h.

410 {
411  T t = first();
412  removeFirst();
413  return t;
414 }
T & first()
Returns a reference to the first item in the list.
Definition: qlinkedlist.h:196
void removeFirst()
Removes the first item in the list.
Definition: qlinkedlist.h:200

◆ takeLast()

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 417 of file qlinkedlist.h.

418 {
419  T t = last();
420  removeLast();
421  return t;
422 }
T & last()
Returns a reference to the last item in the list.
Definition: qlinkedlist.h:198
void removeLast()
Removes the last item in the list.
Definition: qlinkedlist.h:201

◆ toStdList()

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

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

Since
4.1

Example:

list << 1.2 << 0.5 << 3.14;
std::list<double> stdlist = list.toStdList();
See also
fromStdList()

Definition at line 226 of file qlinkedlist.h.

227  { std::list<T> tmp; qCopy(constBegin(), constEnd(), std::back_inserter(tmp)); return tmp; }
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlinkedlist.h:184
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlinkedlist.h:187
OutputIterator qCopy(InputIterator begin, InputIterator end, OutputIterator dest)
Definition: qalgorithms.h:79

Friends and Related Functions

◆ const_iterator

template<class T>
friend class const_iterator
friend

Definition at line 179 of file qlinkedlist.h.

◆ iterator

template<class T>
friend class iterator
friend

Definition at line 149 of file qlinkedlist.h.

◆ operator<<()

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 287 of file qdatastream.h.

288 {
289  s << quint32(l.size());
290  typename QLinkedList<T>::ConstIterator it = l.constBegin();
291  for(; it != l.constEnd(); ++it)
292  s << *it;
293  return s;
294 }
#define it(className, varName)
The QLinkedList::const_iterator class provides an STL-style const iterator for QLinkedList.
Definition: qlinkedlist.h:151
unsigned int quint32
Definition: qglobal.h:938
QFactoryLoader * l

◆ operator>>()

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 270 of file qdatastream.h.

271 {
272  l.clear();
273  quint32 c;
274  s >> c;
275  for(quint32 i = 0; i < c; ++i)
276  {
277  T t;
278  s >> t;
279  l.append(t);
280  if (s.atEnd())
281  break;
282  }
283  return s;
284 }
unsigned char c[8]
Definition: qnumeric_p.h:62
unsigned int quint32
Definition: qglobal.h:938
QFactoryLoader * l

Properties

◆ @57

union { ... }

◆ d

template<class T>
QLinkedListData* QLinkedList< T >::d

◆ e

template<class T>
QLinkedListNode<T>* QLinkedList< T >::e

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