динамический массив
unit DinamicArray;
interface
type
ppointer=^pointer;
TDinamicArray=object
private
DinArr:pointer;
size:longint;
count:longint;
procedure UpSize;
procedure Mirroring(var out:pointer);
procedure WriteItem(adr,p:pointer;i:longint);
public
constructor Init;
procedure AddItem(p:pointer);
function NowCount:longint;
function NowSize:longint;
function ReadItem(num:longint):pointer;
destructor Done;
end;
function AddrP(adr:pointer;i:longint):ppointer;
implementation
function AddrP(adr:pointer;i:longint):ppointer;
begin
AddrP:=ptr(seg(adr),ofs(adr)+(i-1)*SizeOf(pointer));
end;
constructor TDinamicArray.Init;
begin
size:=8;
count:=0;
GetMem(DinArr,SizeOf(pointer)*size);
end;
procedure TDinamicArray.Mirroring(var out:pointer);
var
i:longint;
begin
for i:=1 to (size-8) do
begin
WriteItem(out,ReadItem(i),i);
end;
end;
procedure TDinamicArray.UpSize;
var
aDinArr:pointer;
begin
size:=size+8;
GetMem(aDinArr,SizeOf(pointer)*size);
Mirroring(aDinArr);
FreeMem(DinArr,SizeOf(pointer)*(size-8));
DinArr:=aDinArr;
end;
function TDinamicArray.NowSize:longint;
begin
NowSize:=size;
end;
function TDinamicArray.NowCount:longint;
begin
NowCount:=count;
end;
function TDinamicArray.ReadItem(num:longint):pointer;
begin
ReadItem:=AddrP(DinArr,num)^;
end;
procedure TDinamicArray.WriteItem(adr,p:pointer;i:longint);
begin
AddrP(adr,i)^:=p;
end;
procedure TDinamicArray.AddItem(p:pointer);
begin
if (count+1)>size then UpSize;
count:=count+1;
WriteItem(DinArr,p,count);
end;
destructor TDinamicArray.Done;
begin
FreeMem(DinArr,Size);
end;
end.