Delphi基础算法动态数组类_第1页
Delphi基础算法动态数组类_第2页
Delphi基础算法动态数组类_第3页
Delphi基础算法动态数组类_第4页
Delphi基础算法动态数组类_第5页
已阅读5页,还剩15页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Delphi修炼 基础算法 动态数组类*Copyright (c)*                               个人知识管理系统             

2、;               WangHao of Program Library-文件日志-名称:动态数组新建:2008-07-08 21:00:00备注:实际数组的动态建立,可以将数组初始化为各种数据类型,数组保存数据以最小4字节      为单位进行保存。故在插入值时可以插入各种值,只要值的类型长度不要超过初始化      时数组的数据类型长度,且在

3、读取数组值时需要自己将之转换其对应的数据类型指针,      并取值。      indexof 函数的使用:      1、先在类外面的后面定义一处理函数,并实际它。         Function CompareFunc (aItem:pointer;aElementPtr:pointer):Integer;    

4、60; 2、在要使用的地方定义TwhCompareFunc函数型变量,将上面定义的函数赋给这个变量。      3、在使用是直接使用这个函数型变量,就像其它的变量一样使用。      4、在TwhCompareFunc进行指针型数据比较时,数组保存的数据必须为相同的指针型,         不然在进行指针型数据比较时,会将数据指向不安全区域而出错。使用:在使用时,第一步进行初始化,其参数为动态数组数据类型的大小, 在

5、给动态数组      进行赋值时最好要要保持数据类型一致,虽然保存不同数据类型(类型大小不能大于      在定义的动态数组的数据类型)是可以的,但在读取的时候,如果不能自己记得记录      的数据类型是什么,则就不能正常进行使用。      第二步添加记录,插入记录,在这容量和记录个数会随着自动变化。Add,Insert      第三步可以对

6、记录进行删除和清空操作。Delete,Clear,Remove      第四步除了读取第一条和最后一条记录的函数,还可通过类对象的索引号来读取。      第五步其它操作,如交换,移动等。-编写日志-新建:2008-07-08 21:00:00备注:主要完成了数组操作的初始化、插入、删除、交换、移动、读取和属性,还有出错和      排序处理两个函数没有编写。      出错函数处理结束后,要跳

7、出当前调用的过程或函数。-添加:2008-07-11 20:00:00备注:添加了whError函数,以后还要修改。      在这直接Raise时,函数会直接推到程序结束,直到被except捕获,所有在类中进行      错误处理时,只要直接Raise出错误信息就可以。-修改:2008-07-11 20:00:00备注:在First,Last和whGetItem内Result结果返回了一个指针,在将返回的结果赋给一个     指针变量后,对这个指针变量进

8、行赋值会使动态数组内的值改变。*unit whDynamicArray;interfaceuses  whDefine, SysUtils;type  TwhRecordList = class  private    FActElemSize     : Integer;       /数组数据类型长度    FArray       

9、    : PAnsiChar;     /数组首地址    FCount           : Integer;       /数组当前保存的数据个数    FCapacity        : Integer;  

10、     /数组当前长度(即当前容量)    FElementSize     : Integer;       /数组实际数据类型长度    FMaxElemCount    : Integer;        /数据数据保存最大个数    FName &#

11、160;          : TwhNameString; /名称  protected    Function whGetItem (aIndex:Integer):pointer;    procedure whSetCapacity(aCapacity:Integer);    procedure whSetCount(aCount:Integer);    Proce

12、dure whError(aErrorCode:Integer;                      Const aMethodName:TwhNameString;aIndex:Integer);    procedure whExpand;  public    Constructor Create(aEle

13、mentSize:Integer);    Destructor Destroy;override;    Function Add(aItem:pointer):Integer;    procedure Clear;    procedure Delete(aIndex:integer);    procedure Exchange(aIndex1,aIndex2:Integer);    Function Fi

14、rst:pointer;    Function Indexof(aItem:pointer;aCompare:TwhCompareFunc):Integer;    procedure Insert(aIndex:Integer;aItem:pointer);    Function Last:pointer;    procedure Move(aCurIndex,aNewIndex:Integer);    Function Remove(

15、aItem:pointer;aCompare:TwhCompareFunc):Integer;    procedure Sort(aCompare:TwhCompareFunc); /排序    property Capacity:Integer read FCapacity write whSetCapacity;    property Count:Integer read FCount write whSetCount;    property ElementSize

16、:Integer read FActElemSize;    property ItemsaIndex:Integer:pointer read whGetItem;default;    property MaxCount:Integer read FMaxElemCount;    property Name:TwhNameString read FName write FName;  end;implementation*Copyright (c)*描述:读取Items属性时,进行合格性验

17、证。-编写日志-修改:2008-07-11 21:00:00备注:Result结果返回了一个指针,在将返回的结果赋给一个指针变量后,对这个指针变量     进行赋值会使动态数组内的值改变。*Function TwhRecordList.whGetItem (aIndex:Integer):pointer;begin  if (aIndex<0) or (aIndex>=Count) then    whError(EwhIndexOutOfBounds,'whGetItem',aInde

18、x);  /-修改:2008-07-11 21:00:00  /Result:=pointer(FArray+aIndex*FElementSize);  GetMem(Result,FElementSize);  System.Move(FArray+aIndex*FElementSize),Result,FElementSize);  /-end;*Copyright (c)*描述:设置Capacity属性时,进行合格性验证。-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.wh

19、SetCapacity(aCapacity:Integer);begin  if (aCapacity<>FCapacity) then  begin    /不可忽视的最大元素个数    if (aCapacity > FMaxElemCount) then      whError(EwhCapacityTooLarge,'whSetCapacity',0);    /重分配数组,容量为零时予于释放

20、。    ReallocMem(FArray,aCapacity*FElementSize);    /如果容量缩小,则检查相应数目    if (aCapacity < FCapacity) then    begin      if (Count > aCapacity) then        Count :=aCapacity; 

21、;   end;    /保存新容量    FCapacity:=aCapacity;  end;end;*Copyright (c)*描述:设置Count属性时,进行合格性验证。-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.whSetCount(aCount:Integer);begin  if (aCount <> FCount) then  begin    /如果新的个数

22、大于容量,扩充    if (aCount > Capacity) then      Capacity:=aCount;    /如果新的个数大于原有个数,则新的元素置零    if (aCount > FCount) then      FillChar(FArray+(FCount*FElementSize),(aCount-FCount)*FElementSize,0); &#

23、160;  /保存新的数量    FCount:=aCount;  end;end;*Copyright (c)*描述:输入错误信息。-编写日志-新建:2008-07-08 21:00:00备注:*Procedure TwhRecordList.whError(aErrorCode:Integer;                     

24、;           Const aMethodName:TwhNameString;aIndex:Integer);begin  raise Exception.Create(aMethodName);end;*Copyright (c)*描述:增加动态数组的容量。-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.whExpand;var  NewCapacity:integer;begin  /当前

25、容量为0是,则置当前容量为4个元素  if (Capacity = 0) then    NewCapacity := 4  /如果当前容量小于64,则使当前容量增加16个元素  else if  (Capacity < 64) then    NewCapacity:=Capacity + 16  /如果当前容量大于等于64,则使当前容量增加当前容量的1/4  else    NewCapacity:=Capacity+ (Capacit

26、y Div 4);  /确保不超出数组的上限  if (NewCapacity>FMaxElemCount) then  begin    NewCapacity:=FMaxElemCount;    if NewCapacity = Capacity then      whError(EwhAtMaxCapacity,'whExpand',0);  end;  /设置新容量  Capacity:=Ne

27、wCapacity;end;*Copyright (c)*描述:类初始化。需要参数:aElementSize为元素的大小-编写日志-新建:2008-07-08 21:00:00备注:*Constructor TwhRecordList.Create(aElementSize:Integer);begin  inherited Create;  /保存实际元素大小  FActElemSize:=aElementSize;  /将实际大小取整为最接近的4字节倍数  FElementSize:=(aElementSize +3)shr 2)shl 2

28、;  FMaxElemCount:=MaxInt div FElementSize;end;*Copyright (c)*描述:注销类。删除类分配的区间,通过修改Capacity属性,Capacity属性会通过其写处理      函数来删除类分配的区间。-编写日志-新建:2008-07-08 21:00:00备注:*Destructor TwhRecordList.Destroy;begin  Capacity:=0;  inherited Destroy;end;*Copyright (c)*描述:为动态数组追加

29、一条记录-编写日志-新建:2008-07-08 21:00:00备注:*Function TwhRecordList.Add(aItem:pointer):Integer;begin  Result:=Count;  Insert(Count,aItem);end;*Copyright (c)*描述:清除动态数组内的所有记录-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.Clear;begin  Capacity:=0;end;*Copyright (c)*描述:从动态数组内删除一条记录。参数:aIn

30、dex为被删除记录的索引-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.Delete(aIndex:integer);begin  if (aIndex<0) or (aIndex>=Count) then    whError(EwhIndexOutOfBounds,'Delete',aIndex);  /提前减一,则在(Count-aIndex)中就不用将Count减一后,再在后面加上这条语句  dec(FCount); 

31、0;          if aIndex<Count then    System.Move(FArray+(succ(aIndex)*FElementSize),             (FArray+(aIndex*FElementSize),(Count-aIndex)*FElementSize);end;*Copyright (c)*描述:将两数据交

32、换-编写日志-新建:2008-07-08 21:00:00备注:*procedure TwhRecordList.Exchange(aIndex1,aIndex2:Integer);var  Pemchar:AnsiChar;begin  Pemchar:=#0;  if aIndex1<>aIndex2 then  begin    if (aIndex1<0) or (aIndex1>=Count) then      whError(EwhInd

33、exOutOfBounds,'Exchange',aIndex1);    if (aIndex2<0) or (aIndex2>=Count) then      whError(EwhIndexOutOfBounds,'Exchange',aIndex2);    Pemchar:=(FArray+aIndex1*FElementSize);    System.Move(FArray+(aIndex2*FE

34、lementSize),             (FArray+(aIndex1*FElementSize),FElementSize);    System.Move(Pemchar,             (FArray+(aIndex2*FElementSize),FElementSize);  end;en

35、d;*Copyright (c)*描述:取第一个记录。-编写日志-新建:2008-07-08 21:00:00备注:*Function TwhRecordList.First:pointer;begin  if (FCount<=0) then    whError(EwhNotRecord,'whFirst',0);  /-修改:2008-07-11 21:00:00  /Result:=pointer(FArray);  GetMem(Result,FElementSize);  Syst

36、em.Move(FArray),Result,FElementSize);  /-end;*Copyright (c)*描述:根据记录的值进行比较来返回其符合的记录索引。-编写日志-新建:2008-07-08 21:00:00备注:*Function TwhRecordList.Indexof(aItem:pointer;aCompare:TwhCompareFunc):Integer;var  ElementPtr:PAnsiChar;  i:integer;begin  ElementPtr:=FArray;  for I := 0 to

37、pred(Count) do  begin    if (aCompare(aItem,ElementPtr)=0) then    begin      Result:=i;      Exit;    end;    inc(ElementPtr,FElementSize);  end;  Result:=Ewh_ItemNotPresent;

38、end;*Copyright (c)*描述:为动态数组插入一条记录。参数:aIndex为要插入的位置索引,aItem为要插入的内容。-编写日志-新建:2008-07-08 21:00:00备注:数据的保存是取出参数指针指向的值。*procedure TwhRecordList.Insert(aIndex:Integer;aItem:pointer);begin  if aItem = nil then    whError(EwhNilItem,'Insert',aIndex);  if (aIndex<0) or (aI

39、ndex>Count) then    whError(EwhIndexOutOfBounds,'Insert',aIndex);  if Count = Capacity then    whExpand;  if aIndex<Count then    System.Move(FArray+(aIndex*FElementSize),             (FArray+(succ(aIndex)*FElementSize),(Count-aIndex)*FElementSize);  System.Move(aItem,(FArray+(aIndex*FElementSize),FElementSize);  Inc(FCount);end;*Copyright (c)*描述:取最后一个记录。-编写日志-新建:2008-07-08 21:00:00备注:*Fu

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论