⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list.inc_pas

📁 Delphi Generic Algorytms library - Maps, Lists, Hashmaps, Datastructures.
💻 INC_PAS
📖 第 1 页 / 共 2 页
字号:

  {$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:integer;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:integer;Value: _ValueType);
var
  i: integer;
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:integer;Value: _ValueType);
var
  i: integer;
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 : integer;
  it: _IIterator;
  ContainerSize: integer;
  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 : integer;
  selfSize,ACSize,MinSize : integer;
  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: integer; const Value: _ValueType);
var
  i : integer;
  OldCount : integer;
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:integer);
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):integer;
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:integer);
type
  _PIListIterator  = ^_IListIterator;
var
  i : integer;
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:integer): _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:integer;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:integer):_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_

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -