#include <linkedlist.h>
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 Entity * | currentEntity () |
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 Entity * | entityAt (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 Info * | typeInfo () 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 Entity * | extractCurrentEntity_impl () |
Called by a template method. | |
Protected Attributes | |
Node * | current |
List cursor. | |
Node * | head |
Dummy node that marks the beginning of the list. | |
Node * | tail |
Dummy node that marks the end of the list. |
Derived directly from List, LinkedList is an abstract class that implements most of List using doubly-linked nodes.
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.
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 }
Entity * container::LinkedList::currentEntity | ( | ) | [virtual] |
Return a pointer to the current Entity.
Implements container::List.
bool container::LinkedList::deleteCurrent_impl | ( | ) | [protected, virtual] |
Called by a template method.
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 }
Entity * container::LinkedList::entityAt | ( | int | index | ) | [virtual] |
Return a pointer to the Entity found at the specified, zero-based index.
A valid index must be specified. For example, if count is 1 then the only valid index is 0.
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 }
Entity * container::LinkedList::extractCurrentEntity_impl | ( | ) | [protected, virtual] |
Called by a template method.
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 }
bool container::LinkedList::isFirst | ( | ) | const [virtual] |
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.
Implements container::List.
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.
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.
entity will point to the matching Entity. If a matching Entity is not found, NULL is returned in entity instead.
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 }
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.
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 }
bool container::LinkedList::toFirst | ( | ) | [virtual] |
Point the cursor 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 }
bool container::LinkedList::toLast | ( | ) | [virtual] |
Point the cursor 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 }
bool container::LinkedList::toNext | ( | ) | [virtual] |
Point the cursor to the next Entity in the list.
false if the cursor was moved to the tail of list
false if the cursor was already pointing to the tail
The list cursor is not pointing to the tail
Implements container::List.
bool container::LinkedList::toNext | ( | Node *& | cursor | ) | [protected, virtual] |
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.
false if the cursor is already pointing the first Entity in the list
false if the list is empty
The list cursor is not pointing to the first Entity in the list
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.
Implements container::List.
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 }
Node* container::LinkedList::current [protected] |
List cursor.
Points to the current node
Node* container::LinkedList::head [protected] |
Node* container::LinkedList::tail [protected] |
const Object::Info *const container::LinkedList::TYPE_INFO = Object::typeInfoFactory("LinkedList") [static] |
Reimplemented from container::List.
Reimplemented in container::OrderedLinkedList, and container::SortedLinkedList.