1、TList插入删除内存操作过于频繁,效率不高,可用以于链表替换
2、下链表实现自带了一个Node分配池,以避免内存分配的频率。
(包括:单线链表,双向链表,队列)
//链表节点
pTSingleLinkedNode = ^TSingleLinkedNode;
TSingleLinkedNode = record
private
Next: pTSingleLinkedNode;
public
Data: TObject;
end;
pTMyLinkedNode = ^TMyLinkedNode;
TMyLinkedNode = record
public
Data: TObject;
private
Prev, Next: pTMyLinkedNode;
end;
//链表池
TLinkedNodePool = class
protected
m_Nodes: array of Pointer;
m_Index: Integer;
m_Capacity: Integer;
m_Stoped: Boolean;
function NewNode: Pointer; virtual; abstract;
procedure DisposeNode(p: Pointer); virtual; abstract;
public
constructor Create(nCapacity: Integer = 512); virtual;
destructor Destroy; override;
function GetNode: Pointer;
procedure RetNode(p: Pointer);
procedure ReSize;
procedure Clear;
end;
//单向链表
TSingleLinkedBase = class(TLinkedNodePool)
protected
m_Head: pTSingleLinkedNode;
m_Curr: pTSingleLinkedNode;
m_Count: Integer;
function NewNode: Pointer; override;
procedure DisposeNode(p: Pointer); override;
procedure AddObject(Obj: TObject);
function DeleteObject(Obj: TObject): pTSingleLinkedNode;
public
constructor Create(nCapacity: Integer = 512); override;
destructor Destroy; override;
procedure Clear;
function First: pTSingleLinkedNode;
function Next: pTSingleLinkedNode;
property Count: Integer read m_Count;
end;
//单向对象链表
TSingleObjectLinked = class(TSingleLinkedBase)
public
procedure Add(Obj: TObject);
function Delete(Obj: TObject): pTSingleLinkedNode;
end;
//单向指针链表
TSinglePointerLinked = class(TSingleLinkedBase)
public
procedure Add(Ptr: Pointer);
function Delete(Ptr: Pointer): pTSingleLinkedNode;
end;
//双链表基类,删除比单链表更快
TMyLinkedBase = class(TLinkedNodePool)
protected
m_Head, m_Tail: pTMyLinkedNode;
m_Curr: pTMyLinkedNode;
m_Count: Integer;
function NewNode: Pointer; override;
procedure DisposeNode(p: Pointer); override;
procedure AddObject(Obj: TObject);
function DeleteObject(Obj: TObject): pTMyLinkedNode;
publ