container::LinkedList Class Reference

A mostly complete linked-list implementation of List. More...

#include <linkedlist.h>

Inheritance diagram for container::LinkedList:

Inheritance graph
[legend]
Collaboration diagram for container::LinkedList:

Collaboration graph
[legend]

List of all members.

Classes

class  Node
 LinkedList uses Node objects to form a list of Entity objects. More...

Public Member Functions

virtual ~LinkedList ()
 Free all memory associated with this list.
virtual bool isFirst () const
 Return true if the cursor is pointing to first real node, false otherwise.
virtual bool isLast () const
 Determine whether the list cursor is pointing to the last Entity.
virtual bool isTail () const
 Return true if the cursor is pointing to the list tail, false otherwise.
virtual bool toFirst ()
 Point the cursor to the first Entity in the list.
virtual bool toLast ()
 Point the cursor to the last Entity in the list.
virtual void toTail ()
 Point the cursor to the tail of the list.
virtual bool toNext ()
 Point the cursor to the next Entity in the list.
virtual bool toPrev ()
 Point the cursor to the previous Entity in the list.
virtual EntitycurrentEntity ()
 Return a pointer to the current Entity.
virtual void locateEntity (const Entity &key, int &index, Entity *&entity)
 Locate the first Entity that matches the specified key.
virtual EntityentityAt (int index)
 Return a pointer to the Entity found at the specified, zero-based index.
virtual ostream & renderState (ostream &os) const
 Write the state of an Object for debugging and demonstration.
virtual const InfotypeInfo () const
 Return the instance of Object::Info that describes the class of this object.

Static Public Attributes

static const Info *const TYPE_INFO = Object::typeInfoFactory("LinkedList")

Protected Member Functions

 LinkedList ()
 Creates an empty list.
 LinkedList (const LinkedList &pattern)
 NOT IMPLEMENTED. Creates a deep copy of the pattern list.
virtual bool toNext (Node *&cursor)
virtual bool deleteCurrent_impl ()
 Called by a template method.
virtual EntityextractCurrentEntity_impl ()
 Called by a template method.

Protected Attributes

Nodecurrent
 List cursor.
Nodehead
 Dummy node that marks the beginning of the list.
Nodetail
 Dummy node that marks the end of the list.


Detailed Description

A mostly complete linked-list implementation of List.

Derived directly from List, LinkedList is an abstract class that implements most of List using doubly-linked nodes.


Constructor & Destructor Documentation

container::LinkedList::LinkedList (  )  [protected]

Creates an empty list.

head and tail are allocated and pointed to each other. current is pointed to the tail node.

00049                       : head(new Node()), tail(new Node()) {
00050    current = tail;
00051 
00052    // Point dummy nodes to each other
00053    head->next = tail;
00054    head->prev = NULL;
00055    tail->prev = head;
00056    tail->next = NULL;
00057 }

container::LinkedList::LinkedList ( const LinkedList pattern  )  [protected]

NOT IMPLEMENTED. Creates a deep copy of the pattern list.

Copies of all nodes and their Entities will are created for the new list.

Todo:
Implement copy constructor
00059 { }

container::LinkedList::~LinkedList (  )  [virtual]

Free all memory associated with this list.

Calls purgeContents(), so all Entity objects in the list will be deleted.

00061                         {
00062    purgeContents();
00063    delete head;
00064    delete tail;
00065 }

Here is the call graph for this function:


Member Function Documentation

Entity * container::LinkedList::currentEntity (  )  [virtual]

Return a pointer to the current Entity.

Returns:
A pointer to the current Entity object. Returns NULL if the list was empty or if the cursor was not pointing to an Entity.
Precondition:
The list cursor points to an Entity
Postcondition:
The current Entity remains in the list

The state of the List is unchanged

Implements container::List.

00121                                    {
00122    if (isTail()) return (NULL);
00123    return (current->entity);
00124 }

Here is the call graph for this function:

Here is the caller graph for this function:

bool container::LinkedList::deleteCurrent_impl (  )  [protected, virtual]

Called by a template method.

See also:
Details documented in List::deleteCurrent

Implements container::List.

00159                                     {
00160    if (isTail()) return false;
00161 
00162    Node *doomedNode = current;
00163    current = doomedNode->next;
00164    delete doomedNode;
00165    return true;
00166 }

Here is the call graph for this function:

Entity * container::LinkedList::entityAt ( int  index  )  [virtual]

Return a pointer to the Entity found at the specified, zero-based index.

Returns:
A pointer to the Entity at the specified index
Precondition:
The List must contain at least one Entity object.

A valid index must be specified. For example, if count is 1 then the only valid index is 0.

Postcondition:
The state of the list is unchanged including the cursor position

Implements container::List.

00141                                       {
00142    if ((index >= getCount()) || (index < 0)) {
00143       return NULL;
00144    }
00145 
00146    Node * node = head;
00147    for (int i = 0; toNext(node); i++) {
00148       if (i == index) {
00149          return node->entity;
00150       }
00151    }
00152 
00153    // should never get here
00154    return NULL;
00155 }

Here is the call graph for this function:

Here is the caller graph for this function:

Entity * container::LinkedList::extractCurrentEntity_impl (  )  [protected, virtual]

Called by a template method.

See also:
Details documented in List::extractCurrentEntity

Implements container::List.

00168                                                {
00169    if (isTail()) return NULL;
00170 
00171    Node *doomedNode = current;
00172    current = doomedNode->next;
00173    Entity * entity = doomedNode->extractEntity();
00174    delete doomedNode;
00175    return entity;
00176 }

Here is the call graph for this function:

bool container::LinkedList::isFirst (  )  const [virtual]

Return true if the cursor is pointing to first real node, false otherwise.

TRUE is only possible if the list is not empty.

Implements container::List.

00077                                {
00078    if (current == tail) return (false);
00079    if (current != head->next) return (false);
00080    return (true);
00081 }

bool container::LinkedList::isLast (  )  const [virtual]

Determine whether the list cursor is pointing to the last Entity.

true is only possible if the list is not empty.

Returns:
true if the list cursor is pointing to the last Entity of the list, false otherwise

Implements container::List.

00083                               {
00084    if (tail->prev == current) return (true);
00085    return (false);
00086 }

bool container::LinkedList::isTail (  )  const [virtual]

Return true if the cursor is pointing to the list tail, false otherwise.

false is only possible if the list is not empty.

Implements container::List.

00088                               {
00089    if (current == tail) return (true);
00090    return (false);
00091 }

Here is the caller graph for this function:

void container::LinkedList::locateEntity ( const Entity key,
int &  index,
Entity *&  entity 
) [virtual]

Locate the first Entity that matches the specified key.

The position is returned as a zero based index. The first Entity that matches is returned. No further checking of the list is performed.

Returns:
The zero based index of the matching Entity. If a matching Entity is not found List::NO_MATCHING_ENTITY_INDEX_FLAG is returned in index instead.

entity will point to the matching Entity. If a matching Entity is not found, NULL is returned in entity instead.

Precondition:
A matching Entity is in the list
Postcondition:
The state of the list is unchanged including the cursor position

Implements container::List.

00126                                                                             {
00127    Node * node = head;
00128    int i = 0;
00129    while (toNext(node)) {
00130       if (key.compareKeyTo(*node->entity) == 0) {
00131          index = i;
00132          entity = node->entity;
00133          return;
00134       }
00135       i++;
00136    }
00137    index = NO_MATCHING_ENTITY_INDEX_FLAG;
00138    entity = NULL;
00139 }

Here is the call graph for this function:

ostream & container::LinkedList::renderState ( ostream &  os  )  const [virtual]

Write the state of an Object for debugging and demonstration.

This method must not change the state of an Object; adding or removing debug statements should not change the behavior of a class. The implementation must be robust, e.g., NULL safe, etc. and work without an unrecoverable error for any state, excluding an Object's time of construction and destruction. It is not required for the implementation to be thread safe.

Precondition:
The Object has been fully constructed and is not in the process of destruction
Postcondition:
The state of the Object is unchanged

Reimplemented from container::List.

Reimplemented in container::OrderedLinkedList, and container::SortedLinkedList.

00180                                                    {
00181    this->List::renderState(os);
00182    if (isTail()) {
00183       os << " TAIL<--CURSOR";
00184    }
00185    os << " contents{";
00186    const struct LinkedList::Node* node = head->next;
00187 
00188    for (int i = 0; node != tail; i++) {
00189       if (i > 0) {
00190          os << ", ";
00191       }
00192       const Entity* entity = node->entity;
00193       os << std::endl << "      [" << i << ']' << entity;
00194       if (node == current) {
00195          os << " <--CURSOR";
00196       }
00197       node = node->next;
00198    }
00199 
00200    return os << "}";
00201 }

Here is the call graph for this function:

bool container::LinkedList::toFirst (  )  [virtual]

Point the cursor to the first Entity in the list.

Returns:
false if the list is empty; otherwise, true. A value of true doesn't imply that the cursor was moved, i.e., it may have already been pointing to the first Entity.
Precondition:
The list is not empty
Postcondition:
The list cursor points to the first Entity in the list

Implements container::List.

00093                          {
00094    current = head->next;
00095    if (current == tail) return (false);
00096 
00097    return (true);
00098 }

Here is the caller graph for this function:

bool container::LinkedList::toLast (  )  [virtual]

Point the cursor to the last Entity in the list.

Returns:
false if the list is empty; otherwise, true. A value of true doesn't imply that the cursor was moved, i.e., it may have already been pointing to the last Entity.
Precondition:
The list is not empty
Postcondition:
The list cursor points to the last Entity in the list

Implements container::List.

00100                         {
00101    if (isEmpty()) return (false);
00102    current = tail->prev;
00103 
00104    return (true);
00105 }

Here is the call graph for this function:

bool container::LinkedList::toNext (  )  [virtual]

Point the cursor to the next Entity in the list.

Returns:
true if the list cursor was moved and points to an Entity

false if the cursor was moved to the tail of list

false if the cursor was already pointing to the tail

Precondition:
The List is not empty

The list cursor is not pointing to the tail

Postcondition:
The list cursor has moved to the next Entity or to the tail if it was pointing to the last Entity

Implements container::List.

00111                         {
00112    return toNext(current);
00113 }

Here is the caller graph for this function:

bool container::LinkedList::toNext ( Node *&  cursor  )  [protected, virtual]

/brief Performs the toNext opperation on an arbitrary Node, as opposed to the public toNext() which opperates on the list cursor

00069                                      {
00070    if (cursor == tail) return (false);
00071    cursor = cursor->next;
00072    return (cursor != tail);
00073 }

Here is the caller graph for this function:

bool container::LinkedList::toPrev (  )  [virtual]

Point the cursor to the previous Entity in the list.

Calling toPrev() never leaves the cursor pointing to the head of the list.

Returns:
true if the list cursor was moved

false if the cursor is already pointing the first Entity in the list

false if the list is empty

Precondition:
The list is not empty

The list cursor is not pointing to the first Entity in the list

Postcondition:
The list cursor has moved to the previous Entity

Implements container::List.

00115                         {
00116    if (current == head->next) return (false);
00117    current = current->prev;
00118    return (true);
00119 }

void container::LinkedList::toTail (  )  [virtual]

Point the cursor to the tail of the list.

This method should always succeed. A List should always have the concept a tail, i.e., the node or element just beyond the end of the list. No Entity is available at the tail cursor position.

Precondition:
A List has been constructed
Postcondition:
The list cursor points to the tail

Implements container::List.

00107                         {
00108    current = tail;
00109 }

const Object::Info * container::LinkedList::typeInfo (  )  const [virtual]

Return the instance of Object::Info that describes the class of this object.

Instantiation of Object::Info is controlled by the protected method Object::typeInfoFactory(const string&). Each sub-class of Object should create one and only one instance of Object::Info.

Reimplemented from container::List.

Reimplemented in container::OrderedLinkedList, and container::SortedLinkedList.

00203                                              {
00204    return TYPE_INFO;
00205 }


Member Data Documentation

List cursor.

Points to the current node

Dummy node that marks the beginning of the list.

current never points to head.

Todo:
Considering allowing current to point to head.

Dummy node that marks the end of the list.

current is allowed to point to tail. It does so when the list is empty, when the list has been completely iterated, after toTail() is called, or possibly after toNext() is called.

const Object::Info *const container::LinkedList::TYPE_INFO = Object::typeInfoFactory("LinkedList") [static]

Reimplemented from container::List.

Reimplemented in container::OrderedLinkedList, and container::SortedLinkedList.


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

Generated on Tue Jun 16 23:12:58 2009 by  doxygen 1.5.9