加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
List.inc_pas 15.07 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
(*
* DGL(The Delphi Generic Library)
*
* Copyright (c) 2004
* HouSisong@gmail.com
*
* This material is provided "as is", with absolutely no warranty expressed
* or implied. Any use is at your own risk.
*
* Permission to use or copy this software for any purpose is hereby granted
* without fee, provided the above notices are retained on all copies.
* Permission to modify the code and to distribute modified code is granted,
* provided the above notices are retained, and a notice that the code was
* modified is included with the above copyright notice.
*
*)
//------------------------------------------------------------------------------
// _TList\_TListIteratorʵ
// Create by HouSisong, 2004.09.02
//------------------------------------------------------------------------------
{$ifndef __List_inc_pas_}
{$define __List_inc_pas_}
{$I DGLIntf.inc_pas}
{ _TList }
function _TList.GetSelfObj():TObject;
begin
result:=self;
end;
procedure _TList.Swap(AList:_TList);
var
tmpNode : _TListNodeBase;
begin
if self=AList then exit;
tmpNode:=FItEndNode; FItEndNode:=AList.FItEndNode; AList.FItEndNode:=tmpNode;
end;
function _TList.GetBackValue: _ValueType;
begin
result:=self.FItEndNode.pPrevious.Data;
end;
procedure _TList.SetBackValue(const aValue:_ValueType);
begin
{$ifdef _DGL_ObjValue}
_Assign(self.FItEndNode.pPrevious.Data,aValue);
{$else}
self.FItEndNode.pPrevious.Data:=aValue;
{$endif}
end;
procedure _TList.Clear;
begin
self.DeleteNode(self.FItEndNode.pNext,@self.FItEndNode);
FItEndNode.pNext:=@FItEndNode;
FItEndNode.pPrevious:=@FItEndNode;
end;
constructor _TList.Create;
begin
inherited Create();
self.FItEndNode.pNext:=@self.FItEndNode;
self.FItEndNode.pPrevious:=@self.FItEndNode;
end;
function _TList.Clone: _IContainer;
begin
result:=_IList(_TList.Create(self));
end;
procedure _TList.CloneToInterface(out NewContainer);
begin
_IList(NewContainer):=_TList.Create(self);
end;
constructor _TList.Create(const AList: _TList);
begin
inherited Create();
self.FItEndNode.pNext:=@self.FItEndNode;
self.FItEndNode.pPrevious:=@self.FItEndNode;
self.Insert(self.ItBegin,AList.ItBegin,AList.ItEnd);
end;
constructor _TList.Create(const ItBegin, ItEnd: _IIterator);
begin
inherited Create();
self.FItEndNode.pNext:=@self.FItEndNode;
self.FItEndNode.pPrevious:=@self.FItEndNode;
self.Insert(self.ItBegin,ItBegin, ItEnd);
end;
constructor _TList.Create(const num: _TNativeInt; const Value: _ValueType);
begin
inherited Create();
self.FItEndNode.pNext:=@self.FItEndNode;
self.FItEndNode.pPrevious:=@self.FItEndNode;
self.Insert(self.ItBegin,num,Value);
end;
destructor _TList.Destroy;
begin
self.Clear();
inherited;
end;
procedure _TList.Erase(const ItPos: _IIterator);
begin
ErasePPos(_PListNode(ItPos._Data0));
end;
function _TList.ItBegin: _IIterator;
begin
_TListIterator.ItCreate(result,self.FItEndNode.pNext);
end;
function _TList.GetFrontValue: _ValueType;
begin
result:=self.FItEndNode.pNext.Data;
end;
procedure _TList.SetFrontValue(const aValue:_ValueType);
begin
{$ifdef _DGL_ObjValue}
_Assign(self.FItEndNode.pNext.Data,aValue);
{$else}
self.FItEndNode.pNext.Data:=aValue;
{$endif}
end;
procedure _TList.Insert(const ItPos: _IIterator; const Value: _ValueType);
begin
Insert(_PListNode(ItPos._Data0),Value);
end;
procedure _TList.Insert(const Value:_ValueType);
begin
Insert(@self.FItEndNode,Value);
end;
procedure _TList.Insert(const ItPos:_IIterator;const num:_TNativeInt;const Value:_ValueType);
var
i : _TNativeInt;
begin
for i:=0 to num-1 do
Insert(_PListNode(ItPos._Data0),Value);
end;
procedure _TList.Insert(const ItPos:_IIterator;const ItBegin,ItEnd:_IIterator);
var
It : _IIterator;
begin
It:=ItBegin.Clone();
while (not It.IsEqual(ItEnd)) do
begin
Insert(_PListNode(ItPos._Data0),It.Value);
It.Next;
end;
end;
function _TList.ItEnd: _IIterator;
begin
_TListIterator.ItCreate(result,@self.FItEndNode);
end;
procedure _TList.PopBack;
begin
self.ErasePPos(self.FItEndNode.pPrevious);
end;
procedure _TList.PopFront;
begin
self.ErasePPos(self.FItEndNode.pNext);
end;
procedure _TList.PushBack(const Value: _ValueType);
begin
Insert(@self.FItEndNode,Value);
end;
procedure _TList.PushFront(const Value: _ValueType);
begin
Insert(self.FItEndNode.pNext,Value);
end;
procedure _TList.Reverse;
var
tmpPNode : _PListNode;
pNode : _PListNode;
begin
pNode:=self.FItEndNode.pNext;
FItEndNode.pNext:=FItEndNode.pPrevious;
FItEndNode.pPrevious:=pNode;
while pNode<>@self.FItEndNode do
begin
tmpPNode:=pNode.pNext;
pNode.pNext:=pNode.pPrevious;
pNode.pPrevious:=tmpPNode;
pNode:=tmpPNode;
end;
end;
function _TList.Size: _TNativeInt;
var
PNode : _PListNode;
begin
PNode:=self.FItEndNode.pNext;
result:=0;
while PNode<>@self.FItEndNode do
begin
PNode:=PNode.PNext;
inc(result);
end;
end;
procedure _TList.Splice(const ItPos: _IIterator; AContainer: _IList;
const ACItBegin, ACItEnd: _IIterator);
begin
Transfer(_PListNode(ItPos._Data0),
_PListNode(ACItBegin._Data0),
_PListNode(ACItEnd._Data0));
end;
procedure _TList.Splice(const ItPos: _IIterator; AContainer: _IList;
const ACItPos: _IIterator);
var
pNode: _PListNode;
begin
pNode:=_PListNode(ACItPos._Data0);;
Transfer(_PListNode(ItPos._Data0),
pNode,pNode.pNext);
end;
procedure _TList.Splice(const ItPos: _IIterator; AContainer: _IList);
begin
self.Splice(ItPos,AContainer,AContainer.ItBegin,AContainer.ItEnd);
end;
function _TList.IsEmpty: Boolean;
begin
result:=(@self.FItEndNode=self.FItEndNode.pNext);
end;
class procedure _TList.DeleteNode(PNode: _PListNode);
begin
{$ifdef _DGL_ObjValue}
_Free(PNode.Data);
{$endif}
system.Dispose(PNode);
end;
class procedure _TList.DeleteNode(PNodeBegin, PNodeEnd: _PListNode);
var
tmpPNext : _PListNode;
begin
while PNodeBegin<>PNodeEnd do
begin
tmpPNext:=PNodeBegin.pNext;
self.DeleteNode(PNodeBegin);
PNodeBegin:=tmpPNext;
end;
end;
class function _TList.NewNode(const Value: _ValueType): _PListNode;
begin
//result:=nil;
system.New(result);
{$ifdef _DGL_ObjValue}
result.Data:=_CopyCreateNew(Value);
{$else}
result.Data:=Value;
{$endif}
end;
class procedure _TList.Insert(const PNodePos: _PListNode;
const Value: _ValueType);
var
PtmpNode : _PListNode;
begin
PtmpNode:=self.NewNode(Value);
PtmpNode.pNext:=PNodePos;
PtmpNode.pPrevious:=PNodePos.pPrevious;
PNodePos.pPrevious.pNext:=PtmpNode;
PNodePos.pPrevious:=PtmpNode;
end;
class procedure _TList.ErasePPos(const PNodePos: _PListNode);
begin
PNodePos.pNext.pPrevious:=PNodePos.pPrevious;
PNodePos.pPrevious.pNext:=PNodePos.pNext;
self.DeleteNode(PNodePos);
end;
procedure _TList.Erase(const ItBegin, ItEnd: _IIterator);
var
pBeginNode,pEndNode : _PListNode;
begin
pBeginNode:=_PListNode(ItBegin._Data0);
pEndNode:=_PListNode(ItEnd._Data0);
pEndNode.pPrevious:=pBeginNode.pPrevious;
pBeginNode.pPrevious.pNext:=pEndNode;
self.DeleteNode(pBeginNode,pEndNode);
end;
class procedure _TList.Transfer(const Position, ItBegin,
ItEnd: _PListNode);
var
tmp : _PListNode;
begin
if (Position=ItEnd) then exit;
ItEnd.pPrevious.pNext:=Position;
ItBegin.pPrevious.pNext:=ItEnd;
Position.pPrevious.pNext:=ItBegin;
tmp:=Position.pPrevious;
Position.pPrevious:=ItEnd.pPrevious;
ItEnd.pPrevious:=ItBegin.pPrevious;
ItBegin.pPrevious:=tmp;
end;
constructor _TList.Create(const num: _TNativeInt);
{$ifdef _DGL_ObjValue}
var
_Default_Value : _ValueType;
{$endif}
begin
inherited Create();
self.FItEndNode.pNext:=@self.FItEndNode;
self.FItEndNode.pPrevious:=@self.FItEndNode;
{$ifdef _DGL_ObjValue}
_Default_Value:=_CreateNew();
self.Insert(self.ItBegin,num,_Default_Value);
_Free(_Default_Value);
{$else}
self.Insert(self.ItBegin,num,_NULL_Value);
{$endif}
end;
procedure _TList.Assign(const num:_TNativeInt;const Value: _ValueType);
begin
self.Clear();
self.PushBack(num,Value);
end;
procedure _TList.Assign(const ItBegin,ItEnd:_IIterator);
begin
self.Clear();
self.PushBack(ItBegin,ItEnd);
end;
procedure _TList.PushBack(const num:_TNativeInt;Value: _ValueType);
var
i: _TNativeInt;
begin
for i:=0 to num-1 do
self.PushBack(Value);
end;
procedure _TList.PushBack(const ItBegin,ItEnd: _IIterator);
begin
Insert(self.ItEnd(),ItBegin,ItEnd);
end;
procedure _TList.PushFront(const num:_TNativeInt;Value: _ValueType);
var
i: _TNativeInt;
begin
for i:=0 to num-1 do
self.PushFront(Value);
end;
procedure _TList.PushFront(const ItBegin,ItEnd: _IIterator);
begin
Insert(self.ItBegin(),ItBegin,ItEnd);
end;
function _TList.IsEquals(const AContainer: _IContainer): Boolean;
var
i : _TNativeInt;
it: _IIterator;
ContainerSize: _TNativeInt;
ItSelf : _PListNode;
begin
ContainerSize:=AContainer.Size();
if ContainerSize<>self.Size() then
begin
result:=false;
exit;
end;
it:=AContainer.ItBegin();
ItSelf:=self.FItEndNode.pNext;
for i:=0 to ContainerSize-1 do
begin
{$ifdef _DGL_Compare}
if (not _IsEqual(ItSelf.Data,it.Value)) then
{$else}
if not(ItSelf.Data=it.Value) then
{$endif}
begin
result:=false;
exit;
end
else
begin
it.Next();
ItSelf:=ItSelf.PNext;
end;
end;
result:=true;
end;
function _TList.IsLess(const AContainer: _IContainer): Boolean;
var
i : _TNativeInt;
selfSize,ACSize,MinSize : _TNativeInt;
ItSelf : _PListNode;
it : _IIterator;
begin
selfSize:=self.Size();
ACSize:=AContainer.Size();
MinSize:=ACSize;
if selfSize<MinSize then MinSize:=selfSize;
it:=AContainer.ItBegin();
ItSelf:=self.FItEndNode.pNext;
for i:=0 to MinSize-1 do
begin
{$ifdef _DGL_Compare}
if (_IsLess(it.Value,ItSelf.Data)) then
{$else}
if it.Value<ItSelf.Data then
{$endif}
begin
result:=false;
exit;
end
else
begin
it.Next();
ItSelf:=ItSelf.PNext;
end;
end;
result:=(selfSize<ACSize);
end;
procedure _TList.Resize(const num: _TNativeInt; const Value: _ValueType);
var
i : _TNativeInt;
OldCount : _TNativeInt;
begin
OldCount:=self.Size();
if num<OldCount then
begin
for i:=num to OldCount-1 do
PopBack();
end
else
begin
for i:=OldCount to num-1 do
PushBack(Value);
end;
end;
procedure _TList.Resize(const num:_TNativeInt);
begin
Resize(num,_NULL_Value);
end;
procedure _TList.Sort();
begin
self.Sort(_IsLess);
end;
type
_PListNodeArray=array [0..maxint div (sizeof(_PListNode))-1] of _PListNode;
_PPListNodeArray = ^_PListNodeArray;
TTestBinaryFunction_PListNode=function(const x,y:_PListNode):boolean of object;
type
_TList_TestBinaryFunction=object//class(TObject)
public
TestBinaryFunction : TTestBinaryFunction;
function TestBinaryFunction_PListNode(const x,y:_PListNode):boolean;{$ifdef _DGL_Inline} inline; {$endif}
end;
function _TList_TestBinaryFunction.TestBinaryFunction_PListNode(const x,y:_PListNode):boolean;
begin
result:=TestBinaryFunction(x.Data,y.Data);
end;
type
_TList_TestBinaryFunctionOfObject=object//class(TObject)
public
TestBinaryFunction : TTestBinaryFunctionOfObject;
function TestBinaryFunction_PListNode(const x,y:_PListNode):boolean;{$ifdef _DGL_Inline} inline; {$endif}
end;
function _TList_TestBinaryFunctionOfObject.TestBinaryFunction_PListNode(const x,y:_PListNode):boolean;
begin
result:=TestBinaryFunction(x.Data,y.Data);
end;
{$define _DGL_Sys_Cmp_}
{$I _List_Algorithms_Base.inc_pas}
{$undef _DGL_Sys_Cmp_}
{$define _DGL_Proc_Cmp_}
{$I _List_Algorithms_Base.inc_pas}
{$undef _DGL_Proc_Cmp_}
{$define _DGL_ObjProc_Cmp_}
{$I _List_Algorithms_Base.inc_pas}
{$undef _DGL_ObjProc_Cmp_}
{ _TListIterator }
class procedure _TListIterator.ItCreate(var SelfItData:_IIterator;const pNode: _PListNode);
begin
SelfItData._ObjIteratorClass:=_TListIterator;
_PListNode(SelfItData._Data0):=pNode;
end;
class function _TListIterator.GetValue(const SelfItData:_IIterator): _ValueType;
begin
result:=(_PListNode(SelfItData._Data0)).Data;
end;
class function _TListIterator.IsEqual(const SelfItData:_IIterator;const Iterator: _IIterator): boolean;
begin
result:=(SelfItData._Data0)=(Iterator._Data0);
end;
class procedure _TListIterator.Next(var SelfItData:_IIterator);
begin
_PListNode(SelfItData._Data0):=_PListNode(SelfItData._Data0).pNext;
end;
class procedure _TListIterator.Previous(var SelfItData:_IIterator);
begin
_PListNode(SelfItData._Data0):=_PListNode(SelfItData._Data0).pPrevious;
end;
class procedure _TListIterator.SetValue(const SelfItData:_IIterator;const Value: _ValueType);
begin
{$ifdef _DGL_ObjValue}
_Assign(_PListNode(SelfItData._Data0).Data,Value);
{$else}
_PListNode(SelfItData._Data0).Data:=Value;
{$endif}
end;
class function _TListIterator.Distance(const SelfItData:_IIterator;const Iterator:_IIterator):_TNativeInt;
type
_PIListIterator = ^_IListIterator;
var
it : _IListIterator;
begin
_IIterator(it):=SelfItData;
result:=0;
while it._Data0<>Iterator._Data0 do
begin
inc(result);
it.Next();
end;
end;
class procedure _TListIterator.Next(var SelfItData:_IIterator; const Step:_TNativeInt);
type
_PIListIterator = ^_IListIterator;
var
i : _TNativeInt;
begin
if Step>=0 then
for i:=0 to Step-1 do _PIListIterator(@SelfItData).Next()
else
for i:=0 to (-Step)-1 do _PIListIterator(@SelfItData).Previous();
end;
class function _TListIterator.IteratorTraits():TIteratorTraits;
begin
result:=itBidirectionalTag;
end;
{_IListIterator}
procedure _IListIterator.SetValue(const aValue: _ValueType);
begin
{$ifdef _DGL_ObjValue}
_Assign(_PListNode(Self._Data0).Data,Value);
{$else}
_PListNode(Self._Data0).Data:=Value;
{$endif}
end;
function _IListIterator.GetValue(): _ValueType;
begin
result:=(_PListNode(Self._Data0)).Data;
end;
function _IListIterator.GetNextValue(const Step:_TNativeInt): _ValueType;
var
It : _IListIterator;
begin
it._ObjIteratorClass:=self._ObjIteratorClass;
It._Data0:=self._Data0;
It.Next(Step);
result:=It.Value;
end;
procedure _IListIterator.SetNextValue(const Step:_TNativeInt;const aValue:_ValueType);
var
It : _IListIterator;
begin
it._ObjIteratorClass:=self._ObjIteratorClass;
It._Data0:=self._Data0;
It.Next(Step);
It.Value:=aValue;
end;
function _IListIterator.IsEqual(const Iterator:_IIterator):boolean;
begin
result:=(Self._Data0)=(Iterator._Data0);
end;
procedure _IListIterator.Assign (const Iterator:_IIterator);
begin
Self._ObjIteratorClass:=Iterator._ObjIteratorClass;
Self._Data0:=Iterator._Data0;
end;
procedure _IListIterator.Next();
begin
_PListNode(Self._Data0):=_PListNode(Self._Data0).pNext;
end;
procedure _IListIterator.Previous();
begin
_PListNode(Self._Data0):=_PListNode(Self._Data0).pPrevious;
end;
function _IListIterator.Clone():_IIterator;
begin
result._ObjIteratorClass:=self._ObjIteratorClass;
result._Data0:=self._Data0;
end;
function _IListIterator.Clone(const NextStep:_TNativeInt):_IIterator;
begin
result._ObjIteratorClass:=self._ObjIteratorClass;
result._Data0:=self._Data0;
result.Next(NextStep);
end;
function _IListIterator.IteratorTraits():TIteratorTraits;
begin
result:=itBidirectionalTag;
end;
{$endif } // __List_inc_pas_
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化