The ilist is a template library modeled after the STL list. Instead of allocating a container for the elements, it uses base classes for the elements. This way much less alloc an free operations are needed, which saves a lot of cpu time and memory. Also when iterating thru the list one indirection less is needed. It uses the istd namespace.
template <class T> class list {
...
};
The list itself consisting of a head and tail pointer to a class T.
template <class T> class listElement {
...
};
Used as a base class for elements. This contains the forward and backward linking pointer and a pointer to the list itself.
list()
constructor, clears the list
~list()
destructor, removes all elements from the list
const T * front()
returns front (head) of list.
bool empty()
const T * back()
returns back (tail) of list.
void push_front(T * e)
Add an element to the front (head) of the list.
void push_back(T * e)
Add an element to the back (tail) of the list.
T * pop_front()
Remove an element from the front (head) of the list.
T * pop_back()
Remove an element from the back (tail) of the list.
void insertBefore(T * e, T * before)
Insert the element e before the element identified by 'before' in the list.
void insertBehind(T * e, T * behind)
Insert the element e behind the element identified by 'behind' in the list.
void swap(T * a, T * b)
Swaps the values of the two given listElement objects. The swap function of listElement must be overwritten, also the pointers a and b passed to swap must point to a valid object.
void sort(T * start = NULL, T * end = NULL)
Sorts the list by using quicksort. The sort will be done between start and end. If start is NULL, the start of the list will be used, and if end is NULL, the end of the list. So by calling sort() withouth parameters, the sorting will be done for the whole list. During sorting, the function will call listElement::compare() and listElement::swap(). So both of them must be implemented to not end up with an assertion.
listElement()
virtual ~listElement()
The destructor of a list element removes the element from the list.
const T * goNext()
Returns the next element in the list.
const T * goPrev()
Returns the previous element in the list.
void remove()
Removes the element from the list.
const list * getList()
Returns the list, in which this element is linked.
virtual int compare(T * e)
Will be called from list::sort() to know how to handle the item and the one passed to compare. The function need to return a value less than 0, if this is < e, zero if this is equal to e or a value greater than 0 if this is > e. If you don't whant to call list::sort, there is no need to override this function.
virtual void swap(T * with)
Will be called from list::sort() or list::swap() to swap the instance of the listElement object with the one passed. The implementation of the function is responsible to switch all fields of this and with. If you don't whant to call list::sort() or list::swap(), there is no need to override this function.
// use the ilist base class
class sample : public istd::listElement<sample> {
}
...
// defining the list
istd::list<sample> samples;
...
// iterating thru the list
for(class sample * s = samples.front(); s; s=s->goNext()) {
...
}