PLearn 0.1
Public Member Functions | Public Attributes
PLearn::DoublyLinkedList< T > Class Template Reference

Class description: More...

#include <DoublyLinkedList.h>

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

List of all members.

Public Member Functions

 DoublyLinkedList ()
DoublyLinkedListElement< T > * pushOnTop (T entry)
void moveToFront (DoublyLinkedListElement< T > *element)
void removeLast ()
int nElements ()
virtual ~DoublyLinkedList ()

Public Attributes

DoublyLinkedListElement< T > * first
DoublyLinkedListElement< T > * last
int n_elements

Detailed Description

template<class T>
class PLearn::DoublyLinkedList< T >

Class description:

Doubly linked list of with a 'move-to-front' operation, done in constant time.

Definition at line 79 of file DoublyLinkedList.h.


Constructor & Destructor Documentation

template<class T>
PLearn::DoublyLinkedList< T >::DoublyLinkedList ( ) [inline]

Definition at line 85 of file DoublyLinkedList.h.

: first(0), last(0), n_elements(0) {}
template<class T>
virtual PLearn::DoublyLinkedList< T >::~DoublyLinkedList ( ) [inline, virtual]

Definition at line 151 of file DoublyLinkedList.h.

                                       {
        while (first)
        {
            DoublyLinkedListElement<T>* next = first->next;
            delete first;
            first = next;
        }
    }

Member Function Documentation

template<class T>
void PLearn::DoublyLinkedList< T >::moveToFront ( DoublyLinkedListElement< T > *  element) [inline]

Definition at line 100 of file DoublyLinkedList.h.

    {
#ifdef BOUNDCHECK
        if (dbg) 
            cout << "calling DoublyLinkedList::moveToFront(" << element << endl;
        if (!element) PLERROR("DoublyLinkedList::moveToFront called with NULL element\n");
#endif
        if (element!=first)
        {
            DoublyLinkedListElement<T>* next = element->next;
            DoublyLinkedListElement<T>* previous = element->previous;
            // there must be a previous o/w element would have been first
#ifdef BOUNDCHECK
            if (!previous)
                PLERROR("DoublyLinkedList::moveToFront called on incorrect element or corrupted list\n");
            if (previous->next != element)
                PLERROR("DoublyLinkedList::moveToFront called on element not part of list (previous->next != element)\n");
            if (next && next->previous != element)
                PLERROR("DoublyLinkedList::moveToFront called on element not part of list (next->previous != element)\n");
#endif
            element->next = first;
            element->previous = 0;
            previous->next = next; 
            if (next)
                next->previous = previous;
            first->previous = element;
            first = element;
            if (element==last)
                last = previous;
        }
        
    }
template<class T>
int PLearn::DoublyLinkedList< T >::nElements ( ) [inline]

Definition at line 149 of file DoublyLinkedList.h.

{ return n_elements; }
template<class T>
DoublyLinkedListElement<T>* PLearn::DoublyLinkedList< T >::pushOnTop ( entry) [inline]

Definition at line 87 of file DoublyLinkedList.h.

                                                          {
        if (dbg) 
            cout << "calling DoublyLinkedList::pushOnTop(" << entry << endl;
        DoublyLinkedListElement<T>* new_element = new DoublyLinkedListElement<T>(entry,first);
        if (!last) 
            last=new_element;
        if (first) 
            first->previous=new_element;
        first=new_element;
        n_elements++;
        return new_element;
    }
template<class T>
void PLearn::DoublyLinkedList< T >::removeLast ( ) [inline]

Definition at line 133 of file DoublyLinkedList.h.

                             {
        if (dbg) 
            cout << "calling DoublyLinkedList::removeLast()" << endl;
        if (last)
        {
            DoublyLinkedListElement<T>* before_last = last->previous;
            if (first == last)
                first = 0;
            last->previous=last->next=0; delete last;
            last = before_last;
            if (last)
                last->next = 0;
            n_elements--;
        }
    }

Member Data Documentation

Definition at line 81 of file DoublyLinkedList.h.

Definition at line 82 of file DoublyLinkedList.h.

template<class T>
int PLearn::DoublyLinkedList< T >::n_elements

Definition at line 83 of file DoublyLinkedList.h.


The documentation for this class was generated from the following file:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines