2004-07-22 19:31:08 +00:00
|
|
|
{
|
|
|
|
:: X2UtBinaryTree contains an implementation of the binary tree algorithm,
|
|
|
|
:: along with various descendants which implement support for a range of value
|
2004-07-29 06:33:18 +00:00
|
|
|
:: types (such as pointers, integers and strings). This effectively makes it
|
|
|
|
:: an associative array based on an integer key. For a hash implementation
|
|
|
|
:: based on string keys use the X2UtHashes unit.
|
2004-07-22 19:31:08 +00:00
|
|
|
::
|
|
|
|
:: P.S. I realise that a "B-Tree" is different from a binary tree. For
|
|
|
|
:: convenience reasons I will however ignore your ranting and call my
|
|
|
|
:: classes "TX2UtBTree". ;)
|
|
|
|
::
|
|
|
|
:: Last changed: $Date$
|
|
|
|
:: Revision: $Rev$
|
|
|
|
:: Author: $Author$
|
|
|
|
}
|
|
|
|
unit X2UtBinaryTree;
|
|
|
|
|
|
|
|
interface
|
|
|
|
uses
|
2004-07-29 06:33:18 +00:00
|
|
|
SysUtils,
|
|
|
|
VirtualTrees;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
type
|
|
|
|
//:$ Raised when the cursor is invalid.
|
|
|
|
//:: Call Reset on the binary tree to create a valid cursor.
|
|
|
|
EX2UtBTreeInvalidCursor = class(Exception);
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Internal representation of a node.
|
|
|
|
}
|
|
|
|
PX2UtBTreeNode = ^TX2UtBTreeNode;
|
|
|
|
TX2UtBTreeNode = record
|
|
|
|
Index: Cardinal;
|
|
|
|
Parent: PX2UtBTreeNode;
|
|
|
|
Left: PX2UtBTreeNode;
|
|
|
|
Right: PX2UtBTreeNode;
|
2004-07-29 06:33:18 +00:00
|
|
|
Data: record end;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Internal parent stack
|
|
|
|
}
|
|
|
|
TX2UtBTreeStack = class(TObject)
|
|
|
|
private
|
|
|
|
FItems: array of PX2UtBTreeNode;
|
|
|
|
FCount: Integer;
|
|
|
|
FPosition: Integer;
|
|
|
|
public
|
|
|
|
constructor Create();
|
|
|
|
|
|
|
|
procedure Clear();
|
|
|
|
procedure Push(const ANode: PX2UtBTreeNode);
|
|
|
|
function Pop(): PX2UtBTreeNode;
|
2004-07-23 13:15:14 +00:00
|
|
|
|
|
|
|
procedure Reverse();
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Binary tree implementation
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
:: This class implements a binary tree without knowing anything about
|
|
|
|
:: the data it contains.
|
2004-07-22 19:31:08 +00:00
|
|
|
}
|
2004-07-23 13:15:14 +00:00
|
|
|
TX2UtCustomBTree = class(TObject)
|
2004-07-22 19:31:08 +00:00
|
|
|
private
|
|
|
|
FRoot: PX2UtBTreeNode;
|
|
|
|
FCursor: PX2UtBTreeNode;
|
|
|
|
FIsReset: Boolean;
|
|
|
|
FParent: TX2UtBTreeStack;
|
2004-07-29 06:33:18 +00:00
|
|
|
|
|
|
|
FNodeSize: Cardinal;
|
|
|
|
FDataSize: Cardinal;
|
|
|
|
|
|
|
|
function GetTotalSize(): Cardinal;
|
2004-07-23 13:15:14 +00:00
|
|
|
protected
|
2004-07-22 19:31:08 +00:00
|
|
|
function GetCurrentIndex(): Cardinal;
|
2004-07-23 13:15:14 +00:00
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
function GetNodeData(const ANode: PX2UtBTreeNode): Pointer; virtual;
|
2004-07-22 19:31:08 +00:00
|
|
|
function LookupNode(const AIndex: Cardinal;
|
2004-07-29 06:33:18 +00:00
|
|
|
const ACanCreate: Boolean = False;
|
|
|
|
const ASetCursor: Boolean = False): PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
procedure InitNode(var ANode: PX2UtBTreeNode); virtual;
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure FreeNode(var ANode: PX2UtBTreeNode); virtual;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
procedure ClearCursor(); virtual;
|
2004-07-29 06:33:18 +00:00
|
|
|
function ValidCursor(const ARaiseError: Boolean = True): Boolean; virtual;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
property Cursor: PX2UtBTreeNode read FCursor write FCursor;
|
|
|
|
property Root: PX2UtBTreeNode read FRoot;
|
|
|
|
property IsReset: Boolean read FIsReset write FIsReset;
|
|
|
|
property Parent: TX2UtBTreeStack read FParent;
|
2004-07-23 13:15:14 +00:00
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
property NodeSize: Cardinal read FNodeSize;
|
|
|
|
property TotalSize: Cardinal read GetTotalSize;
|
|
|
|
|
|
|
|
// Note: do NOT change DataSize after the first node has
|
|
|
|
// been created! This will result in an Access Violation!
|
|
|
|
property DataSize: Cardinal read FDataSize write FDataSize;
|
2004-07-23 13:15:14 +00:00
|
|
|
|
|
|
|
//:$ Returns the index at the current cursor location.
|
|
|
|
property CurrentIndex: Cardinal read GetCurrentIndex;
|
2004-07-22 19:31:08 +00:00
|
|
|
public
|
2004-07-29 06:33:18 +00:00
|
|
|
constructor Create(); virtual;
|
2004-07-22 19:31:08 +00:00
|
|
|
destructor Destroy(); override;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
//:$ Clears the tree.
|
2004-07-22 19:31:08 +00:00
|
|
|
procedure Clear();
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
//:$ Deletes a node from the tree.
|
2004-07-22 19:31:08 +00:00
|
|
|
procedure Delete(const AIndex: Cardinal);
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
//:$ Checks if an index exists in the tree.
|
|
|
|
//:: If the ASetCursor parameter is set to True, the cursor will be
|
|
|
|
//:: positioned at the item if it is found.
|
|
|
|
function Exists(const AIndex: Cardinal; const ASetCursor: Boolean = False): Boolean;
|
|
|
|
|
|
|
|
//:$ Resets the node cursor.
|
2004-07-22 19:31:08 +00:00
|
|
|
//:: The node cursor can be used to traverse through the binary tree.
|
|
|
|
//:: Call Reset first, followed by Next to get the first item. You can
|
|
|
|
//:: continue to call Next until it returns false. The CurrentIndex and
|
|
|
|
//:: CurrentValue properties will only be valid within the traversal.
|
|
|
|
//:! Adding or removing items will result in a loss of the current cursor
|
|
|
|
//:! until the next Reset call.
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure Reset(); virtual;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
//:$ Moves the node cursor to the next node.
|
2004-07-22 19:31:08 +00:00
|
|
|
//:! The order in which nodes are traversed is from top to bottom, left
|
|
|
|
//:! to right. Do not depend on the binary tree to sort the output.
|
2004-07-29 06:33:18 +00:00
|
|
|
function Next(): Boolean; virtual;
|
2004-07-23 13:15:14 +00:00
|
|
|
end;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
{
|
2004-07-29 06:33:18 +00:00
|
|
|
:$ Binary tree implementation for pointer values
|
2004-07-23 13:15:14 +00:00
|
|
|
}
|
|
|
|
TX2UtBTree = class(TX2UtCustomBTree)
|
2004-07-29 06:33:18 +00:00
|
|
|
private
|
|
|
|
function GetItem(Index: Cardinal): Pointer;
|
|
|
|
procedure SetItem(Index: Cardinal; const Value: Pointer);
|
|
|
|
|
|
|
|
function GetCurrentValue(): Pointer;
|
2004-07-23 13:15:14 +00:00
|
|
|
public
|
2004-07-29 06:33:18 +00:00
|
|
|
constructor Create(); override;
|
2004-07-23 13:15:14 +00:00
|
|
|
property CurrentIndex;
|
2004-07-29 06:33:18 +00:00
|
|
|
|
|
|
|
//:$ Gets or sets an item.
|
|
|
|
property Items[Index: Cardinal]: Pointer read GetItem
|
|
|
|
write SetItem; default;
|
|
|
|
|
|
|
|
//:$ Returns the value at the current cursor location
|
|
|
|
property CurrentValue: Pointer read GetCurrentValue;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Binary tree implementation for integer values
|
|
|
|
}
|
|
|
|
TX2UtIntegerBTree = class(TX2UtBTree)
|
2004-07-23 13:15:14 +00:00
|
|
|
protected
|
2004-07-22 19:31:08 +00:00
|
|
|
function GetItem(Index: Cardinal): Integer;
|
|
|
|
procedure SetItem(Index: Cardinal; const Value: Integer);
|
|
|
|
function GetCurrentValue(): Integer;
|
|
|
|
public
|
|
|
|
//:$ Gets or sets an item.
|
|
|
|
property Items[Index: Cardinal]: Integer read GetItem
|
|
|
|
write SetItem; default;
|
|
|
|
|
|
|
|
//:$ Returns the value at the current cursor location
|
|
|
|
property CurrentValue: Integer read GetCurrentValue;
|
|
|
|
end;
|
|
|
|
|
|
|
|
{
|
|
|
|
:$ Binary tree implementation for string values
|
|
|
|
}
|
2004-07-29 06:33:18 +00:00
|
|
|
TX2UtStringBTree = class(TX2UtCustomBTree)
|
2004-07-23 13:15:14 +00:00
|
|
|
protected
|
2004-07-22 19:31:08 +00:00
|
|
|
function GetItem(Index: Cardinal): String;
|
|
|
|
procedure SetItem(Index: Cardinal; const Value: String);
|
2004-07-29 06:33:18 +00:00
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
function GetCurrentValue(): String;
|
|
|
|
protected
|
|
|
|
procedure InitNode(var ANode: PX2UtBTreeNode); override;
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure FreeNode(var ANode: PX2UtBTreeNode); override;
|
2004-07-22 19:31:08 +00:00
|
|
|
public
|
2004-07-29 06:33:18 +00:00
|
|
|
constructor Create(); override;
|
|
|
|
property CurrentIndex;
|
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
//:$ Gets or sets an item.
|
|
|
|
property Items[Index: Cardinal]: String read GetItem
|
|
|
|
write SetItem; default;
|
|
|
|
|
|
|
|
//:$ Returns the value at the current cursor location
|
|
|
|
property CurrentValue: String read GetCurrentValue;
|
|
|
|
end;
|
|
|
|
|
2004-07-29 13:48:35 +00:00
|
|
|
resourcestring
|
|
|
|
RSInvalidCursor = 'Cursor is invalid!';
|
|
|
|
RSInvalidDataSize = 'Invalid data size!';
|
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
implementation
|
|
|
|
resourcestring
|
2004-07-29 06:33:18 +00:00
|
|
|
RSOrphanNode = 'BUG: Node does not seem to belong to it''s parent!';
|
|
|
|
RSTooManyPops = 'More Pops than Pushes!';
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
const
|
|
|
|
CStackSize = 32;
|
|
|
|
|
|
|
|
|
|
|
|
{======================== TX2UtBTreeStack
|
|
|
|
Item Management
|
|
|
|
========================================}
|
|
|
|
constructor TX2UtBTreeStack.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
FCount := CStackSize;
|
|
|
|
FPosition := -1;
|
|
|
|
SetLength(FItems, FCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2UtBTreeStack.Clear;
|
|
|
|
begin
|
|
|
|
FCount := CStackSize;
|
|
|
|
FPosition := -1;
|
|
|
|
SetLength(FItems, FCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2UtBTreeStack.Push;
|
|
|
|
begin
|
|
|
|
Inc(FPosition);
|
|
|
|
if FPosition >= FCount then
|
|
|
|
begin
|
|
|
|
Inc(FCount, FCount);
|
|
|
|
SetLength(FItems, FCount);
|
|
|
|
end;
|
|
|
|
|
|
|
|
FItems[FPosition] := ANode;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2UtBTreeStack.Pop;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
if FPosition > -1 then
|
|
|
|
begin
|
|
|
|
Result := FItems[FPosition];
|
|
|
|
Dec(FPosition);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtBTreeStack.Reverse;
|
|
|
|
var
|
|
|
|
iCount: Integer;
|
|
|
|
iIndex: Integer;
|
|
|
|
pSwap: PX2UtBTreeNode;
|
|
|
|
|
|
|
|
begin
|
|
|
|
if FPosition = -1 then
|
|
|
|
exit;
|
|
|
|
|
|
|
|
iCount := (FPosition + 1) div 2;
|
|
|
|
for iIndex := 0 to iCount - 1 do
|
|
|
|
begin
|
|
|
|
pSwap := FItems[iIndex];
|
|
|
|
FItems[iIndex] := FItems[FPosition - iIndex];
|
|
|
|
FItems[FPosition - iIndex] := pSwap;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{======================= TX2UtCustomBTree
|
2004-07-22 19:31:08 +00:00
|
|
|
Initialization
|
|
|
|
========================================}
|
2004-07-23 13:15:14 +00:00
|
|
|
constructor TX2UtCustomBTree.Create;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
FParent := TX2UtBTreeStack.Create();
|
|
|
|
FNodeSize := SizeOf(TX2UtBTreeNode);
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
destructor TX2UtCustomBTree.Destroy;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
|
|
|
FreeAndNil(FParent);
|
2004-07-29 06:33:18 +00:00
|
|
|
|
|
|
|
if Assigned(FRoot) then
|
|
|
|
FreeNode(FRoot);
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
inherited;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
{======================= TX2UtCustomBTree
|
2004-07-22 19:31:08 +00:00
|
|
|
Tree Management
|
|
|
|
========================================}
|
2004-07-29 06:33:18 +00:00
|
|
|
function TX2UtCustomBTree.GetNodeData;
|
|
|
|
begin
|
|
|
|
Assert(DataSize > 0, RSInvalidDataSize);
|
|
|
|
Result := Pointer(Cardinal(ANode) + NodeSize);
|
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
function TX2UtCustomBTree.LookupNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode: PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
if not Assigned(FRoot) then
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
if ACanCreate then
|
2004-07-23 13:15:14 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
InitNode(FRoot);
|
|
|
|
Result := FRoot;
|
|
|
|
|
|
|
|
if ASetCursor then
|
|
|
|
begin
|
|
|
|
Parent.Clear();
|
|
|
|
Cursor := FRoot;
|
|
|
|
end;
|
2004-07-23 13:15:14 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
exit;
|
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode := Root;
|
2004-07-22 19:31:08 +00:00
|
|
|
while Assigned(pNode) do
|
|
|
|
begin
|
|
|
|
if AIndex = pNode^.Index then
|
|
|
|
begin
|
|
|
|
Result := pNode;
|
|
|
|
break;
|
|
|
|
end else if AIndex < pNode^.Index then
|
|
|
|
begin
|
|
|
|
if Assigned(pNode^.Left) then
|
|
|
|
pNode := pNode^.Left
|
|
|
|
else
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
if ACanCreate then
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
InitNode(pNode^.Left);
|
|
|
|
Result := pNode^.Left;
|
|
|
|
Result^.Index := AIndex;
|
|
|
|
Result^.Parent := pNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
if Assigned(pNode^.Right) then
|
|
|
|
pNode := pNode^.Right
|
|
|
|
else
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
if ACanCreate then
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
InitNode(pNode^.Right);
|
|
|
|
Result := pNode^.Right;
|
|
|
|
Result^.Index := AIndex;
|
|
|
|
Result^.Parent := pNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end;
|
2004-07-23 13:15:14 +00:00
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
if ASetCursor and Assigned(Result) then
|
2004-07-23 13:15:14 +00:00
|
|
|
begin
|
|
|
|
// Trace parents
|
2004-07-29 06:33:18 +00:00
|
|
|
Parent.Clear();
|
2004-07-23 13:15:14 +00:00
|
|
|
pNode := Result^.Parent;
|
|
|
|
while Assigned(pNode) do
|
|
|
|
begin
|
|
|
|
Parent.Push(pNode);
|
|
|
|
pNode := pNode^.Parent;
|
|
|
|
end;
|
|
|
|
|
|
|
|
// Parents are now in reverse order
|
|
|
|
Parent.Reverse();
|
|
|
|
end;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtCustomBTree.InitNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
Assert(DataSize > 0, RSInvalidDataSize);
|
|
|
|
GetMem(ANode, TotalSize);
|
|
|
|
FillChar(ANode^, TotalSize, #0);
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure TX2UtCustomBTree.FreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
|
|
|
if Assigned(ANode^.Left) then
|
2004-07-29 06:33:18 +00:00
|
|
|
FreeNode(ANode^.Left);
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
if Assigned(ANode^.Right) then
|
2004-07-29 06:33:18 +00:00
|
|
|
FreeNode(ANode^.Right);
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
if Assigned(ANode^.Parent) then
|
|
|
|
if ANode^.Parent^.Left = ANode then
|
|
|
|
ANode^.Parent^.Left := nil
|
|
|
|
else if ANode^.Parent^.Right = ANode then
|
|
|
|
ANode^.Parent^.Right := nil
|
|
|
|
else
|
|
|
|
Assert(False, RSOrphanNode);
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
FreeMem(ANode, TotalSize);
|
2004-07-22 19:31:08 +00:00
|
|
|
ClearCursor();
|
2004-07-29 06:33:18 +00:00
|
|
|
|
|
|
|
ANode := nil;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtCustomBTree.Clear;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
if Assigned(FRoot) then
|
|
|
|
FreeNode(FRoot);
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtCustomBTree.Delete;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
|
|
|
pItem: PX2UtBTreeNode;
|
|
|
|
|
|
|
|
begin
|
2004-07-23 13:15:14 +00:00
|
|
|
pItem := LookupNode(AIndex);
|
2004-07-22 19:31:08 +00:00
|
|
|
if Assigned(pItem) then
|
2004-07-29 06:33:18 +00:00
|
|
|
FreeNode(pItem);
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
function TX2UtCustomBTree.Exists;
|
|
|
|
begin
|
|
|
|
Result := Assigned(LookupNode(AIndex, False, True));
|
|
|
|
end;
|
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
|
|
|
|
{======================= TX2UtCustomBTree
|
2004-07-22 19:31:08 +00:00
|
|
|
Tree Traversing
|
|
|
|
========================================}
|
2004-07-29 06:33:18 +00:00
|
|
|
function TX2UtCustomBTree.ValidCursor;
|
|
|
|
begin
|
|
|
|
Result := (Assigned(Cursor) and (not IsReset));
|
|
|
|
|
|
|
|
if (not Result) and (ARaiseError) then
|
|
|
|
raise EX2UtBTreeInvalidCursor.Create(RSInvalidCursor);
|
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtCustomBTree.ClearCursor;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
|
|
|
Cursor := nil;
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
procedure TX2UtCustomBTree.Reset;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
|
|
|
Cursor := Root;
|
|
|
|
IsReset := True;
|
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
function TX2UtCustomBTree.Next;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
|
|
|
pParent: PX2UtBTreeNode;
|
|
|
|
pCurrent: PX2UtBTreeNode;
|
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
Result := False;
|
|
|
|
|
2004-07-22 19:31:08 +00:00
|
|
|
if not Assigned(Cursor) then
|
2004-07-29 06:33:18 +00:00
|
|
|
begin
|
|
|
|
IsReset := False;
|
|
|
|
exit;
|
|
|
|
end;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
if not IsReset then
|
|
|
|
begin
|
|
|
|
if Assigned(Cursor^.Left) then
|
|
|
|
begin
|
|
|
|
// Valid left path, follow it
|
|
|
|
Parent.Push(Cursor);
|
|
|
|
Cursor := Cursor^.Left;
|
|
|
|
Result := True;
|
|
|
|
end else if Assigned(Cursor^.Right) then
|
|
|
|
begin
|
|
|
|
// Valid right path, follow it
|
|
|
|
Parent.Push(Cursor);
|
|
|
|
Cursor := Cursor^.Right;
|
|
|
|
Result := True;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
// Neither is valid, traverse back up the parent stack until
|
|
|
|
// a node if found with a sibling
|
|
|
|
pCurrent := Cursor;
|
|
|
|
pParent := Parent.Pop();
|
|
|
|
ClearCursor();
|
|
|
|
|
|
|
|
while Assigned(pParent) do
|
|
|
|
begin
|
|
|
|
if Assigned(pParent^.Right) and (pParent^.Right <> pCurrent) then
|
|
|
|
begin
|
|
|
|
// Parent has a sibling, follow it
|
|
|
|
Parent.Push(pParent);
|
|
|
|
Cursor := pParent^.Right;
|
|
|
|
Result := True;
|
|
|
|
break;
|
|
|
|
end;
|
|
|
|
|
|
|
|
pCurrent := pParent;
|
|
|
|
pParent := Parent.Pop();
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
end else
|
|
|
|
begin
|
|
|
|
IsReset := False;
|
|
|
|
Result := True;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
2004-07-23 13:15:14 +00:00
|
|
|
function TX2UtCustomBTree.GetCurrentIndex;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
Result := 0;
|
|
|
|
if ValidCursor(True) then
|
|
|
|
Result := Cursor^.Index;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
function TX2UtCustomBTree.GetTotalSize;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
Result := FNodeSize + FDataSize;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
{============================= TX2UtBTree
|
|
|
|
Item Management
|
2004-07-22 19:31:08 +00:00
|
|
|
========================================}
|
2004-07-29 06:33:18 +00:00
|
|
|
constructor TX2UtBTree.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
DataSize := SizeOf(Pointer);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2UtBTree.GetItem;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode: PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
|
|
|
Result := nil;
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode := LookupNode(Index);
|
|
|
|
if Assigned(pNode) then
|
|
|
|
Result := PPointer(GetNodeData(pNode))^;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure TX2UtBTree.SetItem;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode: PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode := LookupNode(Index, True);
|
|
|
|
if Assigned(pNode) then
|
|
|
|
PPointer(GetNodeData(pNode))^ := Value;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2UtBTree.GetCurrentValue;
|
|
|
|
begin
|
|
|
|
Result := nil;
|
|
|
|
if ValidCursor(True) then
|
|
|
|
Result := PPointer(GetNodeData(Cursor))^;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{====================== TX2UtIntegerBTree
|
|
|
|
Item Management
|
|
|
|
========================================}
|
|
|
|
function TX2UtIntegerBTree.GetItem;
|
|
|
|
begin
|
|
|
|
Result := Integer(inherited GetItem(Index));
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TX2UtIntegerBTree.SetItem;
|
|
|
|
begin
|
|
|
|
inherited SetItem(Index, Pointer(Value));
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TX2UtIntegerBTree.GetCurrentValue;
|
|
|
|
begin
|
|
|
|
Result := Integer(inherited GetCurrentValue());
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
{======================= TX2UtStringBTree
|
|
|
|
Item Management
|
|
|
|
========================================}
|
2004-07-29 06:33:18 +00:00
|
|
|
constructor TX2UtStringBTree.Create;
|
|
|
|
begin
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
DataSize := SizeOf(PString);
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
|
|
procedure TX2UtStringBTree.InitNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pData: PString;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
inherited;
|
|
|
|
|
|
|
|
pData := GetNodeData(ANode);
|
|
|
|
Initialize(pData^);
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure TX2UtStringBTree.FreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pData: PString;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
pData := GetNodeData(ANode);
|
|
|
|
Finalize(pData^);
|
|
|
|
|
|
|
|
inherited;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
|
|
|
|
function TX2UtStringBTree.GetItem;
|
2004-07-22 19:31:08 +00:00
|
|
|
var
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode: PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode := LookupNode(Index);
|
|
|
|
if Assigned(pNode) then
|
|
|
|
Result := PString(GetNodeData(pNode))^;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
procedure TX2UtStringBTree.SetItem;
|
|
|
|
var
|
|
|
|
pNode: PX2UtBTreeNode;
|
2004-07-22 19:31:08 +00:00
|
|
|
|
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
pNode := LookupNode(Index, True);
|
|
|
|
if Assigned(pNode) then
|
|
|
|
PString(GetNodeData(pNode))^ := Value;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
2004-07-29 06:33:18 +00:00
|
|
|
function TX2UtStringBTree.GetCurrentValue;
|
2004-07-22 19:31:08 +00:00
|
|
|
begin
|
2004-07-29 06:33:18 +00:00
|
|
|
if ValidCursor(True) then
|
|
|
|
Result := PString(GetNodeData(Cursor))^;
|
2004-07-22 19:31:08 +00:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|