QValueList(3qt)

NAME
QValueList - Value based template class that provides doubly linked lists

#include <qvaluelist.h>

  Public Members
QValueList ()
QValueList ( const QValueList<T> & l )
~QValueList ()
QValueList<T>& operator= ( const QValueList<T> & l )
QValueList<T> operator+ ( const QValueList<T> & l ) const
QValueList<T>& operator+= ( const QValueList<T> & l )
bool operator== ( const QValueList<T> & l ) const
bool operator!= ( const QValueList<T> & l ) const
Iterator begin ()
ConstIterator begin () const
Iterator end ()
ConstIterator end () const
Iterator fromLast ()
ConstIterator fromLast () const
bool isEmpty () const
Iterator insert ( Iterator it, const T & x )
Iterator append ( const T & x )
Iterator prepend ( const T & x )
Iterator remove ( Iterator it )
void remove ( const T & x )
T& first ()
const T& first () const
T& last ()
const T& last () const
T& operator[] ( uint i )
const T& operator[] ( uint i ) const
Iterator at ( uint i )
ConstIterator at ( uint i ) const
Iterator find ( const T & x )
ConstIterator find ( const T & x ) const
Iterator find ( Iterator it, const T & x )
ConstIterator find ( ConstIterator it, const T & x ) const
int findIndex ( const T & x ) const
uint contains ( const T & x ) const
uint count () const
void clear ()
QValueList<T>& operator+= ( const T & x )
QValueList<T>& operator<< ( const T & x )

  Protected Members
void detach ()

RELATED FUNCTION DOCUMENTATION
(Note that these are not member functions.)
QDataStream& operator<< (QDataStream & s, const QValueList<T> & l)
QDataStream& operator>> (QDataStream & s, QValueList<T> & l)

DESCRIPTION
The QValueList class is a value based template class that provides doubly linked lists.

Define a template instance QValueList<X> to create a list of values which all have the class X. Please notice that QValueList does not store pointers to the members of the list. It holds a copy of every member. That is the reason why this kind of classes are called "value based" while QList and QDict are "reference based".

Some classes can not be used within a QValueList, for example everything derived from QObject and thus all classes that implement widgets. Only values can be used in a QValueList. To qualify as a value, the class must provide

a copy constructor,

an assignment operator and

a default constructor, i.e. a constructor that does not take any arguments.

Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases, this is sufficient.

Example:

    #include <qvaluelist.h>
    #include <qstring.h>
    #include <stdio.h>
    class Employee
    {
    public:
        Employee(): s(0) {}
        Employee( const QString& name, int salary )
            : n(name), s(salary)
        {}
        QString     name()   const              { return n; }
        int         salary() const              { return s; }
        void        setSalary( int salary )     { s = salary; }
    private:
        QString     n;
        int         s;
    };
    void main()
    {
        typedef QValueList<Employee> EmployeeList;
        EmployeeList list;              // list of Employee
        list.append( Employee("Bill", 50000) );
        list.append( Employee("Steve",80000) );
        list.append( Employee("Ron",  60000) );
        Employee joe( "Joe", 50000 );
        list.append( joe );
        joe.setSalary( 4000 );
        EmployeeList::Iterator it;
        for( it = list.begin(); it != list.end(); ++it )
            printf( "%s earns %d\n", (*it).name().latin1(), (*it).salary().latin1() );
    }

Program output:

        Bill earns 50000
        Steve earns 80000
        Ron earns 60000
        Joe earns 50000

As you can see, the latest changes to Joes salary did not affect the value in the list because the list created a copy of Joes entry.

There are three ways of finding items in the list. The first one is by using the at() function. It returns an iterator. The advantages of getting an iterator is that you can now move forward or backward from this position by incrementing/decrementing the iterator. To get the amount of items in the list call count(). Valid indices are 0..count().

The second way of accessing a list is with operator[]. That means you can address it like an array. The return value is a reference to the value stored in the list. There exist two versions of this operator. The first one is const and returns a const reference to the value. The second on is non const and returns a non const reference to the value. It is up to your compiler to choose the correct one.

The third method is to use the functions begin() and end(). With a simple for loop as shown in the example you can iterate over the complete list. It is save to have multiple iterators at the same time. If some member of the list is removed then only iterators pointing to the removed member become invalid. Inserting in the list does not invalidate any iterator. For convenience the function last() returns an iterator for the last and first() for the first element in the list.

In addition you can search items in the list with the find() function. It exists in a const and a non const version. It starts searching from the beginning of the list, but another flavor of the find() function allows you to specify where searching should start. If you just want to know wether a certain item is at least once in the list, then you can use the contains() function.

Since QValueList is value based there is no need to care about deleting elements in the list. The list holds its own copies and will free them if the corresponding member or the list itself is deleted. You can force the list to free all of its item with clear().

QValueList is implicitly shared. That means you can just make copies of the list in time O(1). If multiple QValueList instances share the same data and one is doing a modification of the lists data then this modifying instance makes a copy and modifies its private copy. So it does not affect the other instances. From a developers point of view you can think that a QValueList and a copy of this list have nothing to do with each other. Developers may only notice that copying is very fast. People known to a CPUs MMU architecture will know this pattern as "copy on write".

There exist three functions to insert items in the list. append() inserts an item at the end, prepend() inserts at the beginning and insert() inserts in front of the position given by an iterator.

Items can be removed from the list in two ways. The first is to pass an iterator to the remove(). The other possibility is to pass a value to remove() which will delete all members which match this value.

Lists can be sorted with the algorithms provided by the Qt Template Library, for example with qHeapSort():

Example:

          QValueList l;
          l.append( 5 );
          l.append( 8 );
          l.append( 3 );
          l.append( 4 );
          qHeapSort( l );

See also QValueListIterator.

MEMBER FUNCTION DOCUMENTATION

QValueList::QValueList ()
Constructs an empty list.

QValueList::QValueList ( const QValueList<T> & l )
Constructs a copy of l.

This operation costs O(1) time since QValueList is implicit shared. The first instance applying modifications to a shared list will create a copy which takes in turn O(n) time. However returning a QValueList from a function is very fast.

QValueList::~QValueList ()
Destroys the list. References to the values in the list and all iterators of this list become invalidated. Since QValueList is highly tuned for performance you wont see warnings if you use invalid iterators, because it is impossible for an iterator to check wether it is valid or not.

Iterator QValueList::append ( const T & x )
Inserts the value x at the end of the list.

Returns an iterator pointing at the inserted item.

See also insert() and prepend().

ConstIterator QValueList::at ( uint i ) const
Returns an iterator pointing to the item at position i in the list, or end() if the index is out of range.

Iterator QValueList::at ( uint i )
Returns an iterator pointing to the item at position i in the list, or end() if the index is out of range.

ConstIterator QValueList::begin () const
Returns an iterator pointing to the first element in the list. This iterator equals end() if the list is empty;

See also first() and end().

Iterator QValueList::begin ()
Returns an iterator pointing to the first element in the list. This iterator equals end() if the list is empty;

See also first() and end().

void QValueList::clear ()
Removes all items from the list.

See also remove().

uint QValueList::contains ( const T & x ) const
Counts and returns the number of occurrences of the value x in the list.

uint QValueList::count () const
Returns the number of items in the list.

See also isEmpty().

void QValueList::detach () [protected]
If the list does not share its data with another QValueList instance, then nothing happens, otherwise the function creates a new copy of this data and detaches from the shared one. This function is called whenever the list is modified. The implicit sharing mechanism is implemented this way.

ConstIterator QValueList::end () const
Returns an iterator pointing behind the last element in the list. This iterator equals begin() if the list is empty.

See also last() and begin().

Iterator QValueList::end ()
Returns an iterator pointing behind the last element in the list. This iterator equals begin() if the list is empty.

See also last() and begin().

ConstIterator QValueList::find ( ConstIterator it, const T & x ) const
Finds the first occurrence of x in the list starting at the position given by it.

Returns end() if no item did match.

ConstIterator QValueList::find ( const T & x ) const
Finds the first occurrence of x in the list.

Returns end() if no item did match.

Iterator QValueList::find ( Iterator it, const T & x )
Finds the first occurrence of x in the list starting at the position given by it.

Returns end() if no item did match.

Iterator QValueList::find ( const T & x )
Finds the first occurrence of x in the list.

Returns end() if no item did match.

int QValueList::findIndex ( const T & x ) const
Returns the first index of the value x in the list or -1 if no such value can be found in the list.

T& QValueList::first ()
Returns a reference to the first item in the list or the item referenced by end() if no such items exists. Please note that you may not change the value the end() Iterator is pointing to.

See also begin() and last().

const T& QValueList::first () const
Returns a reference to the first item in the list or the item referenced by end() if no such items exists.

See also begin() and last().

ConstIterator QValueList::fromLast () const
Returns an iterator pointing to the last element in the list or end() if no such item exists.

See also last().

Iterator QValueList::fromLast ()
Returns an iterator pointing to the last element in the list or end() if no such item exists.

See also last().

Iterator QValueList::insert ( Iterator it, const T & x )
Inserts the value x in front of the iterator it.

Returns an iterator pointing at the inserted item.

See also append() and prepend().

bool QValueList::isEmpty () const
Returns TRUE if the list is empty, i.e. count() == 0. Returns FALSE otherwise.

See also count().

T& QValueList::last ()
Returns a reference to the last item in the list or the item referenced by end() if no such item exists. Please note that you may not change the value the end() Iterator is pointing to.

See also end(), first() and fromLast().

const T& QValueList::last () const
Returns a reference to the last item in the list or the item referenced by end() if no such item exists.

See also end(), first() and fromLast().

bool QValueList::operator!= ( const QValueList<T> & l ) const
Compares both lists.

Returns TRUE if both list are unequal.

QValueList<T> QValueList::operator+ ( const QValueList<T> & l ) const
Creates a new list and fills it with the elements of this list. Then the elements of l are appended.

Returns the new list.

QValueList<T>& QValueList::operator+= ( const QValueList<T> & l )
Adds list to this list.

Returns a reference to this list.

QValueList<T>& QValueList::operator+= ( const T & x )
Adds the value x to the end of the list.

Returns a reference to the list.

QValueList<T>& QValueList::operator<< ( const T & x )
Adds the value x to the end of the list.

Returns a reference to the list.

QValueList<T>& QValueList::operator= ( const QValueList<T> & l )
Assigns l to this list and returns a reference to this list.

All iterators of the current list become invalidated by this operation. The cost of such an assignment is O(1) since QValueList is implicitly shared.

bool QValueList::operator== ( const QValueList<T> & l ) const
Compares both lists.

Returns TRUE if both list are equal.

T& QValueList::operator[] ( uint i )
Returns a reference to the item with index i in the list. It is up to you to check wether this item really exists. You can do that easily with the count() function. However this operator does not check wether i is in range and will deliver undefined results if it does not exist. In contrast to the const operator[] you may manipulate the value returned by this operator.

const T& QValueList::operator[] ( uint i ) const
Returns a const reference to the item with index i in the list. It is up to you to check wether this item really exists. You can do that easily with the count() function. However this operator does not check wether i is in range and will deliver undefined results if it does not exist.

Iterator QValueList::prepend ( const T & x )
Inserts the value x at the beginning of the list.

Returns an iterator pointing at the inserted item.

See also insert() and append().

Iterator QValueList::remove ( Iterator it )
Removes the item at position it in the list.

Returns an iterator pointing to the item following the removed on or end() if the last item was deleted.

See also clear().

void QValueList::remove ( const T & x )
Removes all items which have the value x.

See also clear().

RELATED FUNCTION DOCUMENTATION

QDataStream& operator<< (QDataStream & s, const QValueList<T> & l)
Writes a list to the stream. The type T stored in the list must implement the streaming operator, too.

QDataStream& operator>> (QDataStream & s, QValueList<T> & l)
Reads a list from the stream. The type T stored in the list must implement the streaming operator, too.

SEE ALSO
http://doc.trolltech.com/qvaluelist.html http://www.trolltech.com/faq/tech.html

COPYRIGHT
Copyright 1992-2001 Trolltech AS, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.

AUTHOR
Generated automatically from the source code.

BUGS
If you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. Good bug reports make our job much simpler. Thank you.

In case of content or formattting problems with this manual page, please report them to qt-bugs@trolltech.com. Please include the name of the manual page (qvaluelist.3) and the Qt version (2.3.2).