causerieIntroduction Units Class Hierarchy Classes, Interfaces, Objects and Records Types Variables Constants Functions and Procedures Identifiers Classes hierarchy graph
|
Class ALinkedList
Unit
classwork
Declaration
type ALinkedList = class(APrintingObject, CanStream)
Description
This class represents a doubly-linked list of nodes. It supports accessing the list as if it was an indexed array or as a stack. The list can manage instances of ANode and its descendants.
Hierarchy
Overview
Fields
Methods
Description
Fields
|
MyItemType: ANodeClass; |
Refers to the node type managed by the list
|
|
MyFirstItem: ANode; |
Refers to the first item in the list
|
|
MyLastItem: ANode; |
Refers to the last item in the list
|
|
myCensus: TCensus; |
Stores the current number of items in the list
|
Methods
|
function init: boolean; override; |
Initializer
|
|
destructor destroy; override; |
Destroy the list and its nodes.
This method calls TObject.free on its last node, which will cause all nodes in the list to be freed.
|
|
procedure pushItem(const ThisItem: ANode); virtual; |
Push ThisItem onto the list, as the last node in the list.
A call to ALinkedList.LastItem after this routine finishes will return ThisItem .
|
|
function popItem: ANode; virtual; |
Pop the last item from the list.
|
|
function itemAt(index: TNodeRelativeIndex): ANode; virtual; |
Retrieve the node at the specified index within the list.
This method performs a sequential search of each node in the list; for very large lists, therefore, it can be inefficient.
Index zero (0) represents the first node in the list, while index (ALinkedList.census -1 ) represents the last node in the list. The first and last nodes in the list can also be obtained by calling ALinkedList.FirstItem and ALinkedList.LastItem, respectively.
If index specifies a value that is greater than the number of nodes in the list, then this routine will return the last node in the list.
|
|
procedure insertItemAt(index: TNodeRelativeIndex; const ThisItem: ANode); virtual; |
Insert ThisItem at the specified index within the list.
If a node already exists in the specified position, it "bumped down" to make room for the new item.
Index zero (0) represents the first node in the list, while index (ALinkedList.census -1 ) represents the last node in the list. If index specifies a value that is greater than the number of nodes in the list, then this routine will insert ThisItem at the end of the list.
|
|
function addItem(const ThisItem: ANode): TNodeAbsoluteIndex; virtual; |
Adds thisItem to the end of the list.
This function is nearly identical to ALinkedList.pushItem, except that it returns the index of the node added to the list. This index is not guaranteed to always represent the item, however; as nodes are added and deleted from the list, it is likely that their indices will change.
|
|
function implode(delimiter: string = ''): AnsiString; virtual; |
Condenses the values in the list into a delimited string.
This method uses the value of delimiter to separate the values in the list, which are all condensed into a single string that is returned to the caller. If delimiter is not supplied by the caller, or if it is an empty string, then the value of llstDefaultDelimiter is used.
The base behavior of this method is to iterate through each node in the list and call ANode.toString on them. The values returned are collected into a single (potentially long) string, wherein each value is separated from the next by delimiter . Descendant classes may override this behavior.
|
|
function explode(thisList: AnsiString; delimiter: string = ''): TCensus; virtual; |
Add the items specified in thisList to the list.
This method assumes that thisList contains one or more values that are separated by a delimiter of some kind (e.g., a comma). It uses the value of delimiter to split the string into its component values; these values are then added as individual items to the list. If delimiter is not supplied, or if it is an empty string, then the value of llstDefaultDelimiter is used.
This method does nothing in the base instance of ALinkedList; it is defined as a counterpoint to ALinkedList.implode and to ensure consistent behavior across descendants of ALinkedList.
Returns
The total number of items added to the list. |
|
function removeItemAt(const index: TNodeRelativeIndex): ANode; virtual; |
Removes the node at the specified index from the list.
Index zero (0) represents the first node in the list, while index (ALinkedList.census -1 ) represents the last node in the list. If index specifies a value that is greater than the number of nodes in the list, then this routine will remove the last item in the list.
The node is removed, but is not freed. It is the caller's responsibility to free the node when it is no longer required.
|
|
function deleteItemAt(const index: TNodeRelativeIndex): boolean; virtual; |
Deletes the node at the specified index from the list.
This method calls ALinkedList.removeItemAt to remove the specified node from the list; it then calls TObject.free on that node.
Index zero (0) represents the first node in the list, while index (ALinkedList.census -1 ) represents the last node in the list. If index specifies a value that is greater than the number of nodes in the list, then this routine will remove the last item in the list.
If the node has any children, they will be also be deleted. To prevent this from happening, you should call ALinkedList.removeItemAt to obtain a reference to the node, then call ANode.adoptChildrenOf to reassign its children to another node. You may then free the node.
.)
Returns
True if the node was found and was deleted; False otherwise
|
|
function shallowCopyFrom(const Other: AnObject): boolean; override; |
Construct a shallow copy of the other object.
This method overrides the behavior inherited from AnObject.shallowCopyFrom: it calls that method, then checks to see whether Other is an instance of ALinkedList. If so, it creates a copy of the items in the list by calling AnObject.shallowCopyFrom on each.
The new list items will be of the same type as ALinkedList.ItemType.
Note that the behavior of this method makes it possible to insert a copy of a list into an existing list, as no existing list items are deleted by this routine.
|
|
function streamingLength: TStreamIOSize; virtual; |
Calculate the number of bytes required to stream the list and its nodes.
This method calls ANode.streamingLength and then adds the number of bytes required to stream ALinkedList.census to the result, which is then returned to the caller.
|
|
function writeTo(const Dest: AStream): TStreamIOSize; virtual; |
Write the list and its nodes to the specified stream.
This method first writes the value of ALinkedList.census to Dest ; then it calls ANode.writeTo on the last node in the list, which recursively causes all nodes in the list to be written to the stream.
Returns
The total number of bytes written to Dest . |
|
function toString: AnsiString; override; |
Construct and return a string representation of the list.
The base implementation of this method within ALinkedList returns a string that contains the name of the class and the current value of ALinkedList.census. The format of this string is controlled by llstStringRepresentationSingular or llstStringRepresentationPlural, depending upon the number of nodes in the list.
|
|
function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; override; |
Print a string representation of the list and its nodes to the specified stream.
This method first prints the string representation returned by a call to ALinkedList.toString, then calls ANode.printTo on its last node, which recursively causes the entire list to be printed to Dest .
prefix and suffix are used directly by this method and are not passed to the nodes; instead, this routine passes llstDefaultNodePrintingPrefix and llstDefaultNodePrintingSuffix.
Returns
The total number of bytes printed to Dest . |
|
function ItemType: ANodeClass; virtual; |
Retrieve the type of node managed by the list. This will be a reference to ANode or one of its descendants.
|
|
function census: TCensus; virtual; |
Retrieve the total number of nodes in the list.
The value returned by this function includes the total number of top-level nodes in the list; it does not account for children of those nodes. It is assumed – but not required – that instances of ALinkedList will only contain top-level nodes.
|
|
function FirstItem: ANode; virtual; |
Retrieve a reference to the first node in the list.
If the list has no nodes, this routine will return Nil . The reference returned by this routine should NOT be freed by the caller.
|
|
function LastItem: ANode; virtual; |
Retrieve a reference to the last node in the list.
If the list has no nodes, this routine will return Nil . The reference returned by this routine should NOT be freed by the caller.
|
Generated by PasDoc 0.13.0 on 2015-01-10 17:13:18
|