Class ALinkedList

DescriptionHierarchyFieldsMethodsProperties

Unit

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 (last in, first out). The list can manage instances of ANode and its descendants.

Hierarchy

Overview

Fields

Protected MyItemType: ANodeClass;
Protected MyFirstItem: ANode;
Protected MyLastItem: ANode;
Protected myCensus: TCensus;

Methods

Public function init: boolean; override;
Public destructor destroy; override;
Public procedure pushItem(const ThisItem: ANode); virtual;
Public function PopItem: ANode; virtual;
Public function ItemAt(index: TIndexRelative): ANode; virtual;
Public procedure insertItemAt(index: TIndexRelative; const ThisItem: ANode); virtual;
Public function addItem(const ThisItem: ANode): TIndexRelative; virtual;
Public function implode(delimiter: string = ''): AnsiString; virtual;
Public function explode(thisList: AnsiString; delimiter: string = ''): TCensus; virtual;
Public function RemoveItemAt(const index: TIndexRelative): ANode; virtual;
Public function deleteItemAt(const index: TIndexRelative): boolean; virtual;
Public function shallowCopyFrom(const Other: AnObject): boolean; override;
Public function streamingLength: TStreamIOSize; virtual;
Public function writeTo(const Dest: AStream): TStreamIOSize; virtual;
Public function readFrom(const Source: AStream): TStreamIOSize; virtual;
Public function toString: AnsiString; override;
Public function printTo(const Dest: ATextOutputStream; prefix: AnsiString = ''; suffix: AnsiString = ''): TStreamIOSize; override;
Public function ItemType: ANodeClass; virtual;
Public function census: TCensus; virtual;
Public function FirstItem: ANode; virtual;
Public function LastItem: ANode; virtual;

Description

Fields

Protected MyItemType: ANodeClass;

Refers to the node type managed by the list

Protected MyFirstItem: ANode;

Refers to the first item in the list

Protected MyLastItem: ANode;

Refers to the last item in the list

Protected myCensus: TCensus;

Stores the current number of items in the list

Methods

Public function init: boolean; override;

Initializer

Public 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.

Public 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.

Public function PopItem: ANode; virtual;

Pop the last item from the list.

This method removes the last node from the list and returns it to the caller. The node is not freed; that is for the caller to do.

Public function ItemAt(index: TIndexRelative): 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.

Public procedure insertItemAt(index: TIndexRelative; 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.

Public function addItem(const ThisItem: ANode): TIndexRelative; 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.

Returns

The index of the new item, or -1 if the item was not inserted.

Public 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.

Public 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.

Public function RemoveItemAt(const index: TIndexRelative): 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.

Public function deleteItemAt(const index: TIndexRelative): 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

Public 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.

Public 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.

Public 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.

Public function readFrom(const Source: AStream): TStreamIOSize; virtual;

Read the list and its nodes from the specified stream.

This method first reads the value of ALinkedList.census from Source; it then constructs an instance of ALinkedList.ItemType for each entry in the stream and calls ANode.readFrom on each.

Returns

The total number of bytes read from Source.

Public 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.

Public 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.

Public 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.

Public 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.

Public 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.

Public 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-06-23 19:40:11