Class List.

Inherits Garbage. Inherited by SortedList and StringList.

A generic template class for a doubly-linked list of pointers-to-T.

A new List isEmpty(), and elements can be added using append(), prepend() or insert(). The list knows its first() and last() elements, and can find() specific ones. You can take() any element away, as well as count() or clear() the entire List.

This class never deletes the List elements themselves. That is the caller's responsibility.

There is also a SortedList template.

List::List()

Creates an empty list.

Reimplements Garbage::Garbage().

void List::append( T * d )

Adds d to the end of the List.

void List::clear()

Empties the list by simply forgetting about all of its elements.

uint List::count() const

Returns the number of elements in the list.

Iterator & List::end() const

Returns an Iterator that points beyond the last element in the List. In a boolean context, this Iterator evaluates to false.

Iterator & List::find( const String & d )

Returns an Iterator pointing to the position of the first element in the List that points to an object that is equal to the String d.

Iterator & List::find( const T * d )

Returns an Iterator pointing to the position of the first element in the List that is equal to d.

Iterator & List::first() const

Returns an Iterator that points to the first element in the List. The Iterator evaluates to 0 if the list isEmpty().

T * List::firstElement() const

This function returns the contents of the first element in the List, without the need to create and dereference an Iterator with first(). Returns 0 if the list isEmpty().

void List::insert( const Iterator & i, T * d )

Inserts d before the element pointed to by i.

bool List::isEmpty() const

Returns true only if the List contains no elements.

Iterator & List::last() const

Returns an Iterator that points to the last element in the List. The Iterator evaluates to 0 if the list isEmpty().

T * List::pop()

Removes the last element from the List and returns it, or 0 if there is no element to remove. Equivalent to take( last() ).

void List::prepend( T * d )

Adds d to the beginning of the List.

T * List::remove( const T * d )

This function is equivalent to take( find( d ) ), in that it finds the position of the first element in the List equal to d, then removes that element. Its advantage is that it performs no memory allocation. It returns a pointer to the removed element, or 0 if it was not found in the List.

T * List::shift()

Removes the first element from the List and returns it, or 0 if the list isEmpty(). This is equivalent to take( first() ), but doesn't allocate as much memory.

List<T> * List::sorted( Comparator * comparator )

Returns a list containing the same items as in this list, sorted as comparator says to. comparator has the same meaning as for qsort(3).

T * List::take( Iterator & i )

Removes the element pointed to by i from the List, and returns it after incrementing i to point to the next element in the List. If i does not point to a list element (e.g. end()), take() returns 0 and does nothing.

List::~List()

Destroys the list without affecting its contents.

This web page based on source code belonging to Oryx Mail Systems GmbH. All rights reserved.